VisualC++面向对象与可视化程序设计实验二实验报告一、实验目的1、使用API函数编制包含各种资源的Windows应用程序;2、实现Windows应用程序对键盘与鼠标的响应。二、实验内容1、问题描述:(1)创建一个菜单,其中有三个菜单项,分别为“文件”、“计算”和“帮助”,其中,“文件”菜单项包含“打开”、“保存”、“画图”、“退出”等菜单选项;“计算”菜单包含“总和”、“方差”、“均方根”等菜单选项;“帮助”菜单项包含“计算总和帮助”、“计算方差帮助”和“计算均方根帮助”以及“关于”等项。(2)若单击“画图”菜单项,则绘出P103图形。若在用户区内按下鼠标左键,则动态创建一个包括“删除计算总和”、“添加计算平均值”和“修改计算均方差”三个菜单项的弹出式菜单,由此菜单控制“计算”菜单中菜单项删除、添加与修改。(要求新的弹出式菜单开始时不可用,按下右键后变为可用。)单击“修改计算均方差”菜单项后,“计算”菜单下的“计算均方差”项改为“线性拟合”,单击“添加计算平均值”菜单项后,“计算”菜单中添加上了“计算平均值”菜单项。(3)将光标设定为自己名字中的某个字、图标设定为自己名字中的另一个字。2、应用程序中所用到的资源、消息,简介主要函数的功能;3、程序实现4、操作结果三、程序代码#includewindows.h#includeE08700212叶孙晓.hHMENUhmenu,haddmenu;LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInst,LPSTRlpszCmdLine,intnCmdShow){HWNDhwnd;MSGMsg;HACCELhAccel;WNDCLASSwndclass;charlpszMenuName[]=Menu;charlpszClassName[]=菜单;charlpszTitle[]=E08700212叶孙晓二;wndclass.style=0;wndclass.lpfnWndProc=WndProc;wndclass.cbClsExtra=0;wndclass.cbWndExtra=0;wndclass.hInstance=hInstance;wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.lpszMenuName=lpszMenuName;wndclass.lpszClassName=lpszClassName;if(!RegisterClass(&wndclass)){MessageBeep(0);returnFALSE;}hwnd=CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);hAccel=LoadAccelerators(hInstance,lpszMenuName);while(GetMessage(&Msg,NULL,0,0)){if(!TranslateAccelerator(hwnd,hAccel,&Msg)){TranslateMessage(&Msg);DispatchMessage(&Msg);}}returnMsg.wParam;}LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam){staticbooldraw=false;staticboolpingjunzhiadd=true;staticboolleft=true;HDChdc;PAINTSTRUCTps;HPENhp;HBRUSHhb;switch(message){caseWM_COMMAND:switch(LOWORD(wParam)){caseIDM_EXIT:SendMessage(hwnd,WM_DESTROY,0,0);break;caseIDM_fangchamodify:ModifyMenu(hmenu,IDM_fangcha,MF_BYCOMMAND,IDM_nihe,线性拟合);DrawMenuBar(hwnd);break;caseIDM_pingjunzhiadd:if(pingjunzhiadd){InsertMenu(hmenu,IDM_junfanggen,MF_BYCOMMAND|MF_ENABLED,IDM_pingjunzhi,平均值);DrawMenuBar(hwnd);}pingjunzhiadd=false;break;caseIDM_DRAW:hmenu=GetMenu(hwnd);draw=true;break;}break;caseWM_LBUTTONDOWN:if(left){hmenu=GetMenu(hwnd);haddmenu=CreateMenu();AppendMenu(haddmenu,MF_GRAYED,IDM_zonghedelete,删除计算总和);AppendMenu(haddmenu,MF_GRAYED,IDM_fangchamodify,修改计算方差);AppendMenu(haddmenu,MF_GRAYED,IDM_pingjunzhiadd,添加计算平均值);InsertMenu(hmenu,3,MF_POPUP|MF_BYPOSITION,(UINT)haddmenu,修改菜单);DrawMenuBar(hwnd);}left=false;break;caseWM_RBUTTONDOWN:EnableMenuItem(haddmenu,IDM_zonghedelete,MF_ENABLED);EnableMenuItem(haddmenu,IDM_fangchamodify,MF_ENABLED);EnableMenuItem(haddmenu,IDM_pingjunzhiadd,MF_ENABLED);DrawMenuBar(hwnd);break;caseWM_PAINT:if(draw){hdc=BeginPaint(hwnd,&ps);SetMapMode(hdc,MM_TEXT);Rectangle(hdc,130,60,270,200);hb=CreateHatchBrush(HS_CROSS,RGB(255,0,0));SelectObject(hdc,hb);Ellipse(hdc,130,70,270,190);hp=CreatePen(PS_DASHDOT,1,RGB(0,255,0));SelectObject(hdc,hp);MoveToEx(hdc,100,130,NULL);LineTo(hdc,300,130);MoveToEx(hdc,200,30,NULL);LineTo(hdc,200,230);EndPaint(hwnd,&ps);DeleteObject(hp);DeleteObject(hb);}break;caseWM_DESTROY:PostQuitMessage(0);break;default:returnDefWindowProc(hwnd,message,wParam,lParam);}return0;}头文件#defineIDM_ADDMENU14#defineIDM_DELMENU16#defineIDM_OPEN11#defineIDM_SAVE13#defineIDM_lcw17#defineIDM_DRAW12#defineIDM_EXIT15#defineIDM_zonghe18#defineIDM_fangcha19#defineIDM_junfanggen21#defineIDM_zonghehelp22#defineIDM_fangchahelp23#defineIDM_junfanggenhelp24#defineIDM_ABOUT20#defineIDM_zonghedelete30#defineIDM_fangchamodify31#defineIDM_pingjunzhiadd32#defineIDM_nihe33#defineIDM_pingjunzhi34资源文件//MicrosoftDeveloperStudiogeneratedresourcescript.//#includeresource.h#defineAPSTUDIO_READONLY_SYMBOLS/////////////////////////////////////////////////////////////////////////////////GeneratedfromtheTEXTINCLUDE2resource.//#includeafxres.h#includewindows.h#includeE08700212叶孙晓.hE08700212ICOND:\E08700212二\E08700212.ICOE08700212CURSORD:\E08700212二\E08700212.CURMenuMENUDISCARDABLE{POPUP文件(&F){MENUITEM打开(&O)\tCtrl+O,IDM_OPENMENUITEM画图(&G),IDM_DRAWMENUITEM保存(&S),IDM_SAVEMENUITEM另存为(&A),IDM_lcwMENUITEMSEPARATORMENUITEM退出(&X),IDM_EXIT}POPUP计算(&C){MENUITEM计算总和(&S),IDM_zongheMENUITEM计算方差(&D),IDM_fangchaMENUITEM线性拟合,IDM_junfanggen}POPUP帮助(&H){MENUITEM计算总和的帮助,IDM_zonghehelpMENUITEM计算方差的帮助,IDM_fangchahelpMENUITEM计算均方根的帮助,IDM_junfanggenhelpMENUITEMSEPARATORMENUITEM关于(&A),IDM_ABOUT}/////////////////////////////////////////////////////////////////////////////#undefAPSTUDIO_READONLY_SYMBOLS///////////////////////////////////////////////////////////////////////////////Chinese(中国)resources#if!defined(AFX_RESOURCE_DLL)||defined(AFX_TARG_CHS)#ifdef_WIN32LANGUAGELANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED#pragmacode_page(936)#endif//_WIN32#ifdefAPSTUDIO_INVOKED/////////////////////////////////////////////////////////////////////////////////TEXTINCLUDE//1TEXTINCLUDEDISCARDABLEBEGINresource.h\0END2TEXTINCLUDEDISCARDABLEBEGIN#includeafxres.h\r\n#includewindows.h\r\n#includeE08700212叶孙晓.h\r\nE08700