Windows程序设计实验.docx
《Windows程序设计实验.docx》由会员分享,可在线阅读,更多相关《Windows程序设计实验.docx(35页珍藏版)》请在冰豆网上搜索。
Windows程序设计实验
《Windows程序设计》实验指导书
2009-3-1
目录
实验一:
VC++6.0开发环境熟悉1
实验二:
API对话框应用程序设计3
实验三:
API菜单使用6
实验四、API绘图程序12
实验一:
VC++6.0开发环境熟悉
一、实验目的
熟悉VisualC++6.0开发环境,掌握VC6中建立、维护、运行工程项目的方法,掌握VC6中调试的方法。
二、实验内容
熟悉VisualC++6.0开发环境,如AppWizard、ClassWizard、工具栏、编译、运行普通的C++应用程序等,并能编写一个简单C++应用程序。
熟悉调试过程。
三、实验要求
1、按下图建立一个C++程序,实现菜单选择,可以实现两个整数的加减。
要求:
[1]、建立工程;
[2]、程序调试通过;
[3]、菜单和功能用一个类来实现。
2、制作一个简单的公司职员的管理程序,这个程序要求涉及到类、结构、函数的基本使用。
同时,这个程序涉及到了对VisualStuido集成开发环境的使用,有关这方面的详细内容,请参阅本书的第二单元“VisualC++编程基础”。
[1]、新建项目。
使用VisualC++的应用程序向导生成一个控制台应用程序Demo。
项目类型选择“Win32ConsoleApplication”,在向导的第一步中将程序类型设置成“A"Hello,World!
"application”。
[2]、添加职员信息类。
在集成开发环境的中选择Insert|NewClass菜单进入添加类的对话框,将新增的类名设置成workmate。
之后,分别修改workmate类的定义及实现文件。
[3]、使用职员信息类。
四、思考题
1、如何实现两个复数的加减?
附:
一些小窍门
1)有时候,你可能在编译的时候,计算机突然非法关机了(可能某人不小心碰了电源或你的内存不稳定等原因)。
当你重启机器后打开刚才的项目,重新进行编译,发现VC会崩掉。
你或许以为你的VC编译器坏了,其实不然(你试试编译其它项目,还是好的!
),你只要将项目的.ncb、.opt、.aps、.clw文件以及Debug、Release目录下的所有文件都删掉,然后重新编译就行了。
2)如果你想与别人共享你的源代码项目,但是把整个项目做拷贝又太大。
你完全可以删掉以下文件:
.dsw、.ncb、.opt、.aps、.clw、.plg文件以及Debug、Release目录下的所有文件。
3)当你的Workspace中包含多个Project的时候,你可能不能直观地、一眼看出来哪个是当前项目。
可以如下设置:
Tools->Options->Format,然后在Category中选择Workspacewindow,改变其默认的字体(比如设成Fixedsys)就行了。
4)如何给已有的Project改名字?
将该Project关掉。
然后以文本格式打开.dsp文件,替换原来的Project名字即可。
5)VC6对类成员的智能提示功能很有用,但有时候会失灵。
你可以先关掉项目,将.clw和.ncb删掉,然后重新打开项目,点击菜单项View->ClassWizard,在弹出的对话框中按一下“AddAll”按钮;重新RebuildAll。
应该可以解决问题。
实验二:
API对话框应用程序设计
一、实验目的
掌握VC6中建立API程序的方法。
二、实验内容
练习手工生成APIWindows应用程序框架,以及利用VC的应用程序生成模版AppWizard来生成API程序框架。
三、实验要求
按下图建立一个API程序。
要求:
[1]、手工生成应用程序;
[2]、使用AppWizard自动生成。
四、实验步骤
(一)使用AppWizard自动生成:
1.在“File”菜单中选择“New”,将新建的类型设置成“Projects”。
同时将项目类型设置成“Win32Application”。
2.在应用程序向导的第二步中选择“Atypical"HelloWorld!
"application。
”
3.使用F5快捷键编译程序。
经过以上三个最简单的步骤,一个“Hello,World”程序便制作完毕。
此时可以查看在该项目所在的文件夹下,生成了哪些文件,并应了解这些文件的具体用法
(二)手工生成应用程序:
1.在“File”菜单中选择“New”,将新建的类型设置成“Projects”。
同时将项目类型设置成“Win32Application”。
2.在应用程序向导的第二步中选择“Emptyapplication。
”
3.在“New”选择“C++Source”,按照课本上实例3-1的内容输入程序,。
4.使用F5快捷键编译程序
经过以上三个最简单的步骤,一个“Hello,World”程序便制作完毕。
此时可以查看在该项目所在的文件夹下,生成了哪些文件,并应了解这些文件的具体用法
参考代码:
#include
#include
LRESULTCALLBACKWinSunProc(
HWNDhwnd,//handletowindow
UINTuMsg,//messageidentifier
WPARAMwParam,//firstmessageparameter
LPARAMlParam//secondmessageparameter
);
intWINAPIWinMain(
HINSTANCEhInstance,//handletocurrentinstance
HINSTANCEhPrevInstance,//handletopreviousinstance
LPSTRlpCmdLine,//commandline
intnCmdShow//showstate
)
{WNDCLASSwndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="Computer2009";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls);
HWNDhwnd;
hwnd=CreateWindow("Computer2009","桂林电子科技大学应用科技学院",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return0;
}
LRESULTCALLBACKWinSunProc(
HWNDhwnd,//handletowindow
UINTuMsg,//messageidentifier
WPARAMwParam,//firstmessageparameter
LPARAMlParam//secondmessageparameter
)
{switch(uMsg)
{
caseWM_CHAR:
charszChar[20];
sprintf(szChar,"charis%d",wParam);
MessageBox(hwnd,szChar,"weixin",0);
break;
caseWM_LBUTTONDOWN:
MessageBox(hwnd,"mouseclicked","weixin",0);
HDChdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"Widows程序设计实验",strlen("Widows程序设计实验"));
ReleaseDC(hwnd,hdc);
break;
caseWM_PAINT:
HDChDC;
PAINTSTRUCTps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"程序设计实验",strlen("程序设计实验"));
EndPaint(hwnd,&ps);
break;
caseWM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?
","Compuer",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
caseWM_DESTROY:
PostQuitMessage(0);
break;
default:
returnDefWindowProc(hwnd,uMsg,wParam,lParam);
}
return0;
}
}
五、思考题
1、Window程序和控制台程序有什么区别?
2、如何添加事件处理?
六、常用函数或者结构体参数的定义(详细参见MSDN)
HICONLoadIcon(
HINSTANCEhInstance,//handletoapplicationinstance
LPCTSTRlpIconName//namestringorresourceidentifier
);
HCURSORLoadCursor(
HINSTANCEhInstance,//handletoapplicationinstance
LPCTSTRlpCursorName//nameorresourceidentifier
);
表图标样式
Value
Description
IDI_APPLICATION
Defaultapplicationicon.
IDI_ASTERISK
SameasIDI_INFORMATION.
IDI_ERROR
Hand-shapedicon.
IDI_EXCLAMATION
SameasIDI_WARNING.
IDI_HAND
SameasIDI_ERROR.
IDI_INFORMATION
Asteriskicon.
IDI_QUESTION
Questionmarkicon.
IDI_WARNING
Exclamationpointicon.
IDI_WINLOGO
Windowslogoicon.
HCURSORLoadCursor(
HINSTANCEhInstance,//handletoapplicationinstance
LPCTSTRlpCursorName//nameorresourceidentifier
);
表光标样式
Value
Meaning
IDC_APPSTARTING
Standardarrowandsmallhourglass
IDC_ARROW
Standardarrow
IDC_CROSS
Crosshair
IDC_HAND
Windows 2000:
Hand
IDC_HELP
Arrowandquestionmark
IDC_IBEAM
I-beam
IDC_ICON
Obsoleteforapplicationsmarkedversion4.0orlater.
IDC_NO
Slashedcircle
IDC_SIZE
Obsoleteforapplicationsmarkedversion4.0orlater.UseIDC_SIZEALL.
IDC_SIZEALL
Four-pointedarrowpointingnorth,south,east,andwest
IDC_SIZENESW
Double-pointedarrowpointingnortheastandsouthwest
IDC_SIZENS
Double-pointedarrowpointingnorthandsouth
IDC_SIZENWSE
Double-pointedarrowpointingnorthwestandsoutheast
IDC_SIZEWE
Double-pointedarrowpointingwestandeast
IDC_UPARROW
Verticalarrow
IDC_WAIT
Hourglass
窗口类结构体
typedefstruct_WNDCLASS{
UINTstyle;
WNDPROClpfnWndProc;
intcbClsExtra;
intcbWndExtra;
HINSTANCEhInstance;
HICONhIcon;
HCURSORhCursor;
HBRUSHhbrBackground;
LPCTSTRlpszMenuName;
LPCTSTRlpszClassName;
}WNDCLASS,*PWNDCLASS;
ClassStyles
Theclassstylesdefineadditionalelementsofthewindowclass.TwoormorestylescanbecombinedbyusingthebitwiseOR(|)operator.Toassignastyletoawindowclass,assignthestyletothestylememberoftheWNDCLASSEXstructure.Theclassstylesareasfollows.
表窗口类风格
Style
Action
CS_BYTEALIGNCLIENT
Alignsthewindow'sclientareaonabyteboundary(inthexdirection).Thisstyleaffectsthewidthofthewindowanditshorizontalplacementonthedisplay.
CS_BYTEALIGNWINDOW
Alignsthewindowonabyteboundary(inthexdirection).Thisstyleaffectsthewidthofthewindowanditshorizontalplacementonthedisplay.
CS_CLASSDC
Allocatesonedevicecontexttobesharedbyallwindowsintheclass.Becausewindowclassesareprocessspecific,itispossibleformultiplethreadsofanapplicationtocreateawindowofthesameclass.Itisalsopossibleforthethreadstoattempttousethedevicecontextsimultaneously.Whenthishappens,thesystemallowsonlyonethreadtosuccessfullyfinishitsdrawingoperation.
CS_DBLCLKS
Sendsadouble-clickmessagetothewindowprocedurewhentheuserdouble-clicksthemousewhilethecursoriswithinawindowbelongingtotheclass.
CS_GLOBALCLASS
Specifiesthatthewindowclassisanapplicationglobalclass.Formoreinformation,seeApplicationGlobalClasses.
CS_HREDRAW
Redrawstheentirewindowifamovementorsizeadjustmentchangesthewidthoftheclientarea.
CS_NOCLOSE
DisablesCloseonthewindowmenu.
CS_OWNDC
Allocatesauniquedevicecontextforeachwindowintheclass.
CS_PARENTDC
Setstheclippingrectangleofthechildwindowtothatoftheparentwindowsothatthechildcandrawontheparent.AwindowwiththeCS_PARENTDCstylebitreceivesaregulardevicecontextfromthesystem'scacheofdevicecontexts.Itdoesnotgivethechildtheparent'sdevicecontextordevicecontextsettings.SpecifyingCS_PARENTDCenhancesanapplication'sperformance.
CS_SAVEBITS
Saves,asabitmap,theportionofthescreenimageobscuredbyawindowofthisclass.Whenthewindowisremoved,thesystemusesthesavedbitmaptorestorethescreenimage,includingotherwindowsthatwereobscured.Therefore,thesystemdoesnotsendWM_PAINTmessagestowindowsthatwereobscuredifthememoryusedbythebitmaphasnotbeendiscardedandifotherscreenactionshavenotinvalidatedthestoredimage.
Thisstyleisusefulforsmallwindows(forexample,menusordialogboxes)thataredisplayedbrieflyandthenremovedbeforeotherscreenactivitytakesplace.Thisstyleincreasesthetimerequiredtodisplaythewindow,becausethesystemmustfirstallocatememorytostorethebitmap.
CS_VREDRAW
Redrawstheentirewindowifamovementorsizeadjustmentchangestheheightoftheclientarea.
创建窗口函数:
HWNDCreateWindow(
LPCTSTRlpClassName,//registeredclassname
LPCTSTRlpWindowName,//windowname
DWORDdwStyle,//windowstyle
intx,//horizontalpositionofwindow
inty,//verticalpositionofwindow
intnWidth,//windowwidth
intnHeight,//windowheight
HWNDhWndParent,//handletoparentorownerwindow
HMENUhMenu,//menuhandleorchildidentifier
HINSTANCEhInstance,//handletoapplicationinstance
LPVOIDlpParam//window-creationdata
);
WindowStyles
Thefollowingstylescanbespecifiedwhereverawindowstyleisrequired.Afterthecontrolhasbeencreated,thesestylescannotbemodified,exceptasnoted.
表窗口样式
Style
Meaning
WS_BORDER
Createsawindowthathasathin-lineborder.
WS_CAPTION
Createsawindowthathasatitlebar(includestheWS_BORDERstyle).
WS_CHILD
Createsachildwindow.Awindowwiththisstylecannothaveamenubar.ThisstylecannotbeusedwiththeWS_POPUPstyle.
WS_CHILDWINDOW
SameastheWS_CHILDstyle.
WS_CLIPC