Windows程序设计实验

上传人:无*** 文档编号:162791271 上传时间:2022-10-19 格式:DOC 页数:26 大小:151KB
返回 下载 相关 举报
Windows程序设计实验_第1页
第1页 / 共26页
Windows程序设计实验_第2页
第2页 / 共26页
Windows程序设计实验_第3页
第3页 / 共26页
点击查看更多>>
资源描述
Windows程序设计实验指导书2009-3-1目录实验一:VC+6.0开发环境熟悉1实验二: API对话框应用程序设计3实验三:API菜单使用6实验四、API绘图程序12实验一:VC+6.0开发环境熟悉一、 实验目的熟悉Visual C+ 6.0开发环境,掌握VC6中建立、维护、运行工程项目的方法,掌握VC6中调试的方法。二、 实验内容熟悉Visual C+ 6.0开发环境,如AppWizard、ClassWizard、工具栏、编译、运行普通的C+应用程序等,并能编写一个简单C+应用程序。熟悉调试过程。三、 实验要求1、按下图建立一个C+程序,实现菜单选择,可以实现两个整数的加减。要求:1、建立工程;2、程序调试通过;3、菜单和功能用一个类来实现。2、制作一个简单的公司职员的管理程序,这个程序要求涉及到类、结构、函数的基本使用。同时,这个程序涉及到了对Visual Stuido集成开发环境的使用,有关这方面的详细内容,请参阅本书的第二单元“Visual C+编程基础”。1、新建项目。使用Visual C+的应用程序向导生成一个控制台应用程序Demo。项目类型选择“Win32 Console Application”,在向导的第一步中将程序类型设置成“A Hello, World! application”。2、添加职员信息类。 在集成开发环境的中选择Insert|New Class菜单进入添加类的对话框,将新增的类名设置成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中选择Workspace window,改变其默认的字体(比如设成Fixedsys)就行了。4) 如何给已有的Project改名字?将该Project关掉。然后以文本格式打开.dsp文件,替换原来的Project名字即可。5) VC6对类成员的智能提示功能很有用,但有时候会失灵。你可以先关掉项目,将.clw和.ncb删掉,然后重新打开项目,点击菜单项View-ClassWizard,在弹出的对话框中按一下“Add All”按钮;重新Rebuild All。应该可以解决问题。实验二: API对话框应用程序设计一、 实验目的掌握VC6中建立API程序的方法。二、 实验内容练习手工生成API Windows应用程序框架,以及利用VC的应用程序生成模版AppWizard来生成API程序框架。三、 实验要求按下图建立一个API程序。要求:1、手工生成应用程序;2、使用AppWizard自动生成。四、 实验步骤(一)使用AppWizard自动生成:1在“File”菜单中选择“New”,将新建的类型设置成“Projects”。同时将项目类型设置成“Win32 Application”。 2在应用程序向导的第二步中选择“A typical Hello World! application。” 3使用F5快捷键编译程序。 经过以上三个最简单的步骤,一个“Hello,World”程序便制作完毕。此时可以查看在该项目所在的文件夹下,生成了哪些文件,并应了解这些文件的具体用法(二)手工生成应用程序:1在“File”菜单中选择“New”,将新建的类型设置成“Projects”。同时将项目类型设置成“Win32 Application”。 2在应用程序向导的第二步中选择“Empty application。” 3在“New”选择“C+Source”,按照课本上实例3-1的内容输入程序,。 4使用F5快捷键编译程序 经过以上三个最简单的步骤,一个“Hello,World”程序便制作完毕。此时可以查看在该项目所在的文件夹下,生成了哪些文件,并应了解这些文件的具体用法参考代码:#include #include LRESULT CALLBACK WinSunProc( HWND hwnd, / handle to window UINT uMsg, / message identifier WPARAM wParam, / first message parameter LPARAM lParam / second message parameter);int WINAPI WinMain( HINSTANCE hInstance, / handle to current instance HINSTANCE hPrevInstance, / handle to previous instance LPSTR lpCmdLine, / command line int nCmdShow / show state)WNDCLASS wndcls;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);HWND hwnd;hwnd=CreateWindow(Computer2009,桂林电子科技大学应用科技学院,WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,SW_SHOWNORMAL);UpdateWindow(hwnd);MSG msg;while(GetMessage(&msg,NULL,0,0)TranslateMessage(&msg);DispatchMessage(&msg);return 0;LRESULT CALLBACK WinSunProc( HWND hwnd, / handle to window UINT uMsg, / message identifier WPARAM wParam, / first message parameter LPARAM lParam / second message parameter)switch(uMsg)case WM_CHAR:char szChar20;sprintf(szChar,char is %d,wParam);MessageBox(hwnd,szChar,weixin,0);break;case WM_LBUTTONDOWN:MessageBox(hwnd,mouse clicked,weixin,0);HDC hdc;hdc=GetDC(hwnd);TextOut(hdc,0,50,Widows程序设计实验,strlen(Widows程序设计实验);ReleaseDC(hwnd,hdc);break;case WM_PAINT:HDC hDC;PAINTSTRUCT ps;hDC=BeginPaint(hwnd,&ps);TextOut(hDC,0,0, 程序设计实验,strlen(程序设计实验);EndPaint(hwnd,&ps);break;case WM_CLOSE:if(IDYES=MessageBox(hwnd,是否真的结束?,Compuer,MB_YESNO)DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd,uMsg,wParam,lParam);return 0;五、 思考题1、 Window程序和控制台程序有什么区别?2、 如何添加事件处理?六、常用函数或者结构体参数的定义(详细参见MSDN)HICON LoadIcon( HINSTANCE hInstance, / handle to application instance LPCTSTR lpIconName / name string or resource identifier);HCURSOR LoadCursor( HINSTANCE hInstance, / handle to application instance LPCTSTR lpCursorName / name or resource identifier);表 图标样式ValueDescriptionIDI_APPLICATIONDefault application icon.IDI_ASTERISKSame as IDI_INFORMATION.IDI_ERRORHand-shaped icon.IDI_EXCLAMATIONSame as IDI_WARNING.IDI_HANDSame as IDI_ERROR. IDI_INFORMATIONAsterisk icon.IDI_QUESTIONQuestion mark icon.IDI_WARNINGExclamation point icon.IDI_WINLOGOWindows logo icon.HCURSOR LoadCursor( HINSTANCE hInstance, / handle to application instance LPCTSTR lpCursorName / name or resource identifier);表 光标样式ValueMeaningIDC_APPSTARTINGStandard arrow and small hourglassIDC_ARROWStandard arrowIDC_CROSSCrosshairIDC_HANDWindows2000: HandIDC_HELPArrow and question markIDC_IBEAMI-beamIDC_ICONObsolete for applications marked version 4.0 or later.IDC_NOSlashed circleIDC_SIZEObsolete for applications marked version 4.0 or later. Use IDC_SIZEALL.IDC_SIZEALLFour-pointed arrow pointing north, south, east, and westIDC_SIZENESWDouble-pointed arrow pointing northeast and southwestIDC_SIZENSDouble-pointed arrow pointing north and southIDC_SIZENWSEDouble-pointed arrow pointing northwest and southeastIDC_SIZEWEDouble-pointed arrow pointing west and eastIDC_UPARROWVertical arrowIDC_WAITHourglass窗口类结构体typedef struct _WNDCLASS UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; WNDCLASS, *PWNDCLASS; Class StylesThe class styles define additional elements of the window class. Two or more styles can be combined by using the bitwise OR (|) operator. To assign a style to a window class, assign the style to the style member of the WNDCLASSEX structure. The class styles are as follows. 表 窗口类风格StyleActionCS_BYTEALIGNCLIENTAligns the windows client area on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.CS_BYTEALIGNWINDOWAligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.CS_CLASSDCAllocates one device context to be shared by all windows in the class. Because window classes are process specific, it is possible for multiple threads of an application to create a window of the same class. It is also possible for the threads to attempt to use the device context simultaneously. When this happens , the system allows only one thread to successfully finish its drawing operation. CS_DBLCLKSSends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is within a window belonging to the class. CS_GLOBALCLASSSpecifies that the window class is an application global class. For more information, see Application Global Classes.CS_HREDRAWRedraws the entire window if a movement or size adjustment changes the width of the client area.CS_NOCLOSEDisables Close on the window menu.CS_OWNDCAllocates a unique device context for each window in the class. CS_PARENTDCSets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with the CS_PARENTDC style bit receives a regular device context from the systems cache of device contexts. It does not give the child the parents device context or device context settings. Specifying CS_PARENTDC enhances an applications performance. CS_SAVEBITSSaves, as a bitmap, the portion of the screen image obscured by a window of this class. When the window is removed, the system uses the saved bitmap to restore the screen image, including other windows that were obscured. Therefore, the system does not send WM_PAINT messages to windows that were obscured if the memory used by the bitmap has not been discarded and if other screen actions have not invalidated the stored image. This style is useful for small windows (for example, menus or dialog boxes) that are displayed briefly and then removed before other screen activity takes place. This style increases the time required to display the window, because the system must first allocate memory to store the bitmap.CS_VREDRAWRedraws the entire window if a movement or size adjustment changes the height of the client area.创建窗口函数:HWND CreateWindow( LPCTSTR lpClassName, / registered class name LPCTSTR lpWindowName, / window name DWORD dwStyle, / window style int x, / horizontal position of window int y, / vertical position of window int nWidth, / window width int nHeight, / window height HWND hWndParent, / handle to parent or owner window HMENU hMenu, / menu handle or child identifier HINSTANCE hInstance, / handle to application instance LPVOID lpParam / window-creation data);Window StylesThe following styles can be specified wherever a window style is required. After the control has been created, these styles cannot be modified, except as noted. 表 窗口样式StyleMeaningWS_BORDERCreates a window that has a thin-line border.WS_CAPTIONCreates a window that has a title bar (includes the WS_BORDER style).WS_CHILDCreates a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.WS_CHILDWINDOWSame as the WS_CHILD style.WS_CLIPCHILDRENExcludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.WS_CLIPSIBLINGSClips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated. If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.WS_DISABLEDCreates a window that is initially disabled. A disabled window cannot receive input from the user. To change this after a window has been created, use EnableWindow. WS_DLGFRAMECreates a window that has a border of a style typically used with dialog boxes. A window with this style cannot have a title bar.WS_GROUPSpecifies the first control of a group of controls. The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style. The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group. The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys. You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.WS_HSCROLLCreates a window that has a horizontal scroll bar.WS_ICONICCreates a window that is initially minimized. Same as the WS_MINIMIZE style.WS_MAXIMIZECreates a window that is initially maximized.WS_MAXIMIZEBOXCreates a window that has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified. WS_MINIMIZECreates a window that is initially minimized. Same as the WS_ICONIC style.WS_MINIMIZEBOXCreates a window that has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified. WS_OVERLAPPEDCreates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_TILED style.WS_OVERLAPPEDWINDOWCreates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_TILEDWINDOW style. WS_POPUPCreates a pop-up window. This style cannot be used with the WS_CHILD style.WS_POPUPWINDOWCreates a pop-up window with WS_BORDER, WS_POPUP, and WS_SYSMENU styles. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.WS_SIZEBOXCreates a window that has a sizing border. Same as the WS_THICKFRAME style.WS_SYSMENUCreates a window that has a window menu on its title bar. The WS_CAPTION style must also be specified.WS_TABSTOPSpecifies a control that can receive the keyboard focus when the user presses the TAB key. Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style. You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.WS_THICKFRAMECreates a window that has a sizing border. Same as the WS_SIZEBOX style.WS_TILEDCreates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_OVERLAPPED style. WS_TILEDWINDOWCreates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_OVERLAPPEDWINDOW style. WS_VISIBLECreates a window that is initially visible. This style can be turned on and off by using ShowWindow or SetWindowPos.WS_VSCROLLCreates a window that has a vertical scroll bar.显示窗口BOOL ShowWindow( HWND hWnd, / handle to window int nCmdShow / show state);ParametershWnd in Handle to the window. nCmdShow in Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values. ValueMeaningSW_FORCEMINIMIZEWindows 2000: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread.SW_HIDEHides the window and activates another window.SW_MAXIMIZEMaximizes the specified window.SW_MINIMIZEMinimizes the specified window and activates the next top-level window in the Z order.SW_RESTOREActivates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.SW_SHOWActivates the window and displays it in its current size and position. SW_SHOWDEFAULTSets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. SW_SHOWMAXIMIZEDActivates the window and displays it as a maximized window.SW_SHOWMINIMIZEDActivates the window and displays it as a minimized window.SW_SHOWMINNOACTIVEDisplays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.SW_SHOWNADisplays the window in its current size and position. This value is similar to SW_SHOW, except the window is not activated.SW_SHOWNOACTIVATEDisplays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except the window is not actived.SW_SHOWNORMALActivates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.一、 补充知识VC+的工程文件说明: *.dsp:是VC+的项目文件,文本格式。 *.dsw:是工作区文件,它可以指向一个或多个.dsp文件。 *.clw:是 ClassWizard信息文件,实际上是INI文件的格式。 *.opt:工程关于开发环境的参数文件,如工具条位置等信息。 *.aps:(AppStudio File),资源辅助文件,二进制格式。 *.rc:资源文件。 *.plg:是编译信息文件,编译时的error和warning信息文件,在Tools-Options里面有个选项可以控制这个文件的生成。 *.hpj:(Help Project)是生成帮助文件的工程,用microsfot Help Compiler可以处理。 *.mdp:(Microsoft DevStudio Project)是旧版本的项目文件,如果要打开此文件的话,会提示你是否转换成新的DSP格式。 *.bsc:是用于浏览项目信息的,如果用Source Brower的话就必须有这个文件。如果不用这个功能的话,可以在Project Options里面去掉Generate Browse Info File,可以加快编译速度。 *.map:是执行文件的映像信息纪录文件,除非对系统底层非常熟悉,这个文件一般用不着。 *.pch:(Pre-Compiled File)是预编译文件,可以加快编译速度,但是文件非常大。 *.pdb:(Program Database)记录了程序有关的一些数据和调试信息,在调试的时候可能有用。 *.exp:只有在编译DLL的时候才会生成,记录了DLL文件中的一些信息。一般也没什么用。 *.ncb:无编译浏览文件(no compile browser)。当自动完成功能出问题时可以删除此文件,build后会自动生成。 *.c:源代码文件,按C语言用法编译处理。 *.cpp:源代码文件,按C+语法编译处理。实验三:API菜单使用一、 实验目的掌握VC6中菜单的创建和使用。二、 实验内容菜单的创建有三种方法:1,在WINDOWCLASS中指定菜单资源的标识符;2,在CreateWindowEx函数参数中指定菜单句柄;3,先用LoadMenu函数载入菜单资源,再用SetMenu函数把菜单加载到应用程序的菜单栏。菜单的操作包含:向已存在菜单添加新的菜单项,删除菜单中的某一项,使菜单项呈现被选中状态,使菜单项无效,创建浮动式菜单,向Windows系统菜单中添加,删除选项等等。三、 实验要求按下图建立一个创建菜单应用的程序框架。要求:1、 创建弹出式菜单2、 显示相关信息3、 向已有菜单中插入新项4、 删除菜单中的选项5、 设置菜单项为选中标志6、 设置菜单项为单选标志7、 向系统菜单中插入新项四、 实验步骤1、新建项目。使用Visual C+的应用程序向导生成一个应用程序MenuDemo。项目类型选择“Win32 Application”,在向导的第一步中将程序类型设置成“A simple Win32 application”。2、编写实现菜单使用的代码。参考代码:#define WIN32_LEAN_AND_MEAN#include stdafx.h#include resource.h#define ID_NEWITEM 1013/定义新菜单项的标识符#define ID_NEWSYSITEM 60441/定义新系统菜单项标识符LRESULT CALLBACK MainProc(HWND,UINT,WPARAM,LPARAM);BOOL WINAPI DoDispPopupMenu(HWND,int,int);/创建弹出式菜单VOID WINAPI DoDispMessage(int);/显示相关信息VOID WINAPI DoInsertMenuItem(void);/向已有菜单中插入新项VOID WINAPI DoDeleteMenuItem(void);/删除菜单中的选项VOID WINAPI DoCheckMenuItem(void);/设置菜单项为选中标志VOID WINAPI DoCheckRadio(void);/设置菜单项为单选标志VOID WINAPI DoInsertSysMenu(void);/向系统菜单中插入新项char szAppName=MenuDemo;HINSTANCE hIns;HWND hMainWnd;BOOL isInsert;/插入菜单标志BOOL isCheck;/选中标志int idRadio=2;/单选标志int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)MSG msg;HWND hWnd;WNDCLASSEX wc;wc.cbClsExtra=0;wc.cbSize=sizeof(WNDCLASSEX);wc.cbWndExtra=0;wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wc.hCursor=LoadCursor(NULL,IDC_ARROW);wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);wc.hIconSm=0;wc.hInstance=hInstance;wc.lpfnWndProc=(WNDPROC)MainProc;wc.lpszClassName=szAppName;wc.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);wc.style=CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC;ReGISterClassEx(&wc);hWnd=CreateWindowEx(0,szAppName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);hMainWnd=hWnd;hIns=hInstance;DoInsertSysMenu();/在此处向系统菜单中插入菜单项ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);while(GetMessage(&msg,NULL,0,0)TranslateMessage(&msg);DispatchMessage(&msg);return msg.wParam;LRESULT CALLBACK MainProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)HDC hdc;PAINTSTRUCT ps;switch(message)case WM_CREATE:return 0;case WM_PAINT:hdc=BeginPaint(hWnd,&ps);EndPaint(hWnd,&ps); /do drawingreturn 0;case WM_RBUTTONDOW
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!