您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 实验3API菜单使用
-1-实验报告3课程名称:Windows编程实验名称:API菜单使用学生姓名:班级:学号:成绩:一、实验目的掌握VC6中菜单的创建和使用。二、实验内容菜单的创建有三种方法:1,在WINDOWCLASS中指定菜单资源的标识符;2,在CreateWindowEx函数参数中指定菜单句柄;3,先用LoadMenu函数载入菜单资源,再用SetMenu函数把菜单加载到应用程序的菜单栏。菜单的操作包含:向已存在菜单添加新的菜单项,删除菜单中的某一项,使菜单项呈现被选中状态,使菜单项无效,创建浮动式菜单,向Windows系统菜单中添加,删除选项等等。三、实验要求按下图建立一个创建菜单应用的程序框架。要求:(1)创建弹出式菜单(2)显示相关信息(3)向已有菜单中插入新项(4)删除菜单中的选项-2-(5)设置菜单项为选中标志(6)设置菜单项为单选标志(7)向系统菜单中插入新项四、实验步骤(一)新建项目使用VisualC++的应用程序向导生成一个应用程序MenuDemo。项目类型选择“Win32Application”,在向导的第一步中将程序类型设置成“AsimpleWin32application”。(二)编写实现菜单使用的代码参考代码://test.cpp:Definestheentrypointfortheapplication.//#defineWIN32_LEAN_AND_MEAN#includestdafx.h#includeresource.h#defineID_NEWITEM1013//定义新菜单项的标识符#defineID_NEWSYSITEM60441//定义新系统菜单项标识符LRESULTCALLBACKMainProc(HWND,UINT,WPARAM,LPARAM);BOOLWINAPIDoDispPopupMenu(HWND,int,int);//创建弹出式菜单VOIDWINAPIDoDispMessage(int);//显示相关信息VOIDWINAPIDoInsertMenuItem(void);//向已有菜单中插入新项VOIDWINAPIDoDeleteMenuItem(void);//删除菜单中的选项VOIDWINAPIDoCheckMenuItem(void);//设置菜单项为选中标志VOIDWINAPIDoCheckRadio(void);//设置菜单项为单选标志VOIDWINAPIDoInsertSysMenu(void);//向系统菜单中插入新项charszAppName[]=MenuDemo;HINSTANCEhIns;HWNDhMainWnd;BOOLisInsert;//插入菜单标志BOOLisCheck;//选中标志intidRadio=2;//单选标志intAPIENTRYWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){MSGmsg;HWNDhWnd;WNDCLASSEXwc;wc.cbClsExtra=0;wc.cbSize=sizeof(WNDCLASSEX);wc.cbWndExtra=0;wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wc.hCursor=LoadCursor(NULL,IDC_ARROW);-3-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);}returnmsg.wParam;}LRESULTCALLBACKMainProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam){HDChdc;PAINTSTRUCTps;switch(message){caseWM_CREATE:return0;caseWM_PAINT:hdc=BeginPaint(hWnd,&ps);EndPaint(hWnd,&ps);//dodrawingreturn0;caseWM_RBUTTONDOWN://鼠标在窗口中右键被按下时,弹出浮动式菜单if(DoDispPopupMenu(hWnd,LOWORD(lParam),HIWORD(lParam)))return0;//函数调用成功elsebreak;//函数调用失败caseWM_COMMAND:switch(LOWORD(wParam)){caseID_INSERT:if(isInsert==0){DoInsertMenuItem();//插入新的菜单项DoDispMessage(4);//显示相关信息-4-}break;caseID_NEWITEM:DoDispMessage(3);break;caseID_DELETE:if(isInsert!=0){DoDeleteMenuItem();//删除菜单项DoDispMessage(5);}break;caseIDEXIT:PostQuitMessage(0);break;caseID_CHECK://在菜单选项前打钩DoCheckMenuItem();break;caseID_RADIO1://在菜单选项前做一圆形标志,表明此项被选中,清除旧的标志idRadio=2;DoCheckRadio();break;caseID_RADIO2://同上idRadio=3;DoCheckRadio();break;caseID_RADIO3://同上idRadio=4;DoCheckRadio();break;caseID_FLOAT1://浮动菜单的选项被选中时的处理DoDispMessage(0);break;caseID_FLOAT2://同上DoDispMessage(1);break;caseID_FLOAT3://同上DoDispMessage(2);break;}return0;caseWM_CLOSE:PostQuitMessage(0);return0;}returnDefWindowProc(hWnd,message,wParam,lParam);}BOOLWINAPIDoDispPopupMenu(HWNDhWnd,intx,inty){HMENUhMenu;HMENUhPopupMenu;POINTpt={x,y};//从资源文件中载入菜单资源hMenu=LoadMenu(hIns,MAKEINTRESOURCE(IDR_MENU2));//获得子菜单的句柄hPopupMenu=GetSubMenu(hMenu,0);-5-//窗口坐标转化到屏幕坐标ClientToScreen(hMainWnd,&pt);//创建浮动菜单if(TrackPopupMenu(hPopupMenu,TPM_LEFTALIGN|TPM_RIGHTBUTTON,pt.x,pt.y,0,hMainWnd,NULL)==FALSE)returnFALSE;//使用完毕,回收内存DestroyMenu(hMenu);returnTRUE;}VOIDWINAPIDoDispMessage(intid){HDChdc;//在菜单事件发生时,输出相应的字符串char*mess[]={CommandFloatingMenuitem1,CommandFloatingMenuitem2,CommandFloatingMenuitem3,CommandNewMenuItem,NewItemHaveBeenInserted,NewItemHaveBeenDeleted};InvalidateRect(hMainWnd,NULL,FALSE);hdc=GetDC(hMainWnd);TextOut(hdc,0,0,mess[id],strlen(mess[id]));ReleaseDC(hMainWnd,hdc);}VOIDWINAPIDoInsertMenuItem(){HMENUhMenu;//插入菜单项HMENUhPopupMenu;MENUITEMINFOinfo;info.cbSize=sizeof(MENUITEMINFO);//每一个新插入的菜单项都必须填写MENUITEMINFO结构info.fMask=MFT_STRING|MIIM_DATA|MIIM_ID|MIIM_TYPE;info.fType=MFT_STRING;info.fState=0;info.wID=ID_NEWITEM;info.hSubMenu=NULL;info.hbmpChecked=NULL;info.hbmpUnchecked=NULL;info.dwItemData=ID_NEWITEM;info.dwTypeData=NewItem;info.cch=8;hMenu=GetMenu(hMainWnd);//获得已存在菜单的句柄hPopupMenu=GetSubMenu(hMenu,0);//获得子菜单的句柄InsertMenuItem(hPopupMenu,1,TRUE,&info);//插入菜单项isInsert=1;//置插入标志为1}-6-VOIDWINAPIDoDeleteMenuItem(){HMENUhMenu;//删除菜单项HMENUhPopupMenu;hMenu=GetMenu(hMainWnd);hPopupMenu=GetSubMenu(hMenu,0);RemoveMenu(hPopupMenu,ID_NEWITEM,MF_BYCOMMAND);isInsert=0;}VOIDWINAPIDoCheckMenuItem(){//此函数先判断菜单项是否被打钩,若没有,则做打钩标记,若已有,则删除打钩标记HMENUhMenu;HMENUhPopupMenu;hMenu=GetMenu(hMainWnd);hPopupMenu=GetSubMenu(hMenu,1);if(isCheck){CheckMenuItem(hPopupMenu,0,MF_BYPOSITION|MF_UNCHECKED);isCheck=0;}else{CheckMenuItem(hPopupMenu,0,MF_BYPOSITION|MF_CHECKED);isCheck=1;}}VOIDWINAPIDoCheckRadio(){HMENUhMenu;HMENUhPopupMenu;hMenu=GetMenu(hMainWnd);hPopupMenu=GetSubMenu(hMenu,1);CheckMenuRadioItem(hPopupMenu,2,4,idRadio,MF_BYPOSITION);}V
本文标题:实验3API菜单使用
链接地址:https://www.777doc.com/doc-2500167 .html