可视化实验二

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验二学号:班级:姓名:一、实验内容1.创建一个菜单,其中有三个菜单项,分别为“文件”、“计算”和“帮助”,其中,“文件”菜单项包含“打开”、“保存”、“画图”、“退出”等菜单选项;“计算”菜单包含“总和”、“方差”、“均方根”等菜单选项;“帮助”菜单项包含“计算总和帮助”、“计算方差帮助”和“计算均方根帮助”以及“关于”等项。2.若单击“画图”菜单项,则绘出P103图形。3.若在用户区内按下鼠标左键,则动态创建一个包括“删除计算总和”、“添加计算平均值”和“修改计算均方差”三个菜单项的弹出式菜单,由此菜单控制“计算”菜单中菜单项删除、添加与修改。(要求新的弹出式菜单开始时不可用,按下右键后变为可用。)单击“修改计算均方差”菜单项后,“计算”菜单下的“计算均方差”项改为“线性拟合”,单击“添加计算平均值”菜单项后,“计算”菜单中添加上了“计算平均值”菜单项。4.将光标设定为自己名字中的某个字、图标设定为自己名字中的另一个字。二、实验仪器电脑一台三、实验原理(主要程序代码及解释)1、创建菜单:定义菜单:在资源描述文件中定义。POPUP语句,并可以包含子菜单。在头文件中定义菜单项标识所对应的数值。加载菜单资源。在窗口类中加载菜单:wcex.lpszMenuName动态的创建菜单:调用CreatMenu创建空的弹出式菜单,调用AppendMenu插入菜单项禁止和激活菜单项MF_ENABLED、MF_GRAYED修改菜单项:ModifyMenu(hmenu,IDM_FANGCHA,MF_BYCOMMAND,IDM_nihe,线性拟合);2、画图:WM_PAINT3、鼠标点击事件:WM_LBUTTONDOWN、WM_RBUTTONDOWN4、光标和图标:创建资源ico、cur在cpp文件中调用wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));5、代码:final.cpp#includestdafx.h#includefinal.h#defineMAX_LOADSTRING100HMENUhmenu,haddmenu;HINSTANCEhInst;成绩TCHARszTitle[MAX_LOADSTRING];TCHARszWindowClass[MAX_LOADSTRING];ATOMMyRegisterClass(HINSTANCEhInstance);BOOLInitInstance(HINSTANCE,int);LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);INT_PTRCALLBACKAbout(HWND,UINT,WPARAM,LPARAM);intAPIENTRY_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPTSTRlpCmdLine,intnCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);MSGmsg;HACCELhAccelTable;LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);LoadString(hInstance,IDC_FINAL,szWindowClass,MAX_LOADSTRING);MyRegisterClass(hInstance);if(!InitInstance(hInstance,nCmdShow)){returnFALSE;}hAccelTable=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_FINAL));while(GetMessage(&msg,NULL,0,0)){if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}return(int)msg.wParam;}ATOMMyRegisterClass(HINSTANCEhInstance){WNDCLASSEXwcex;wcex.cbSize=sizeof(WNDCLASSEX);wcex.style=CS_HREDRAW|CS_VREDRAW;wcex.lpfnWndProc=WndProc;wcex.cbClsExtra=0;wcex.cbWndExtra=0;wcex.hInstance=hInstance;wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));wcex.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR2));wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName=MAKEINTRESOURCE(IDC_FINAL);wcex.lpszClassName=szWindowClass;wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_ICON1));returnRegisterClassEx(&wcex);}BOOLInitInstance(HINSTANCEhInstance,intnCmdShow){HWNDhWnd;hInst=hInstance;hWnd=CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);if(!hWnd){returnFALSE;}ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);returnTRUE;}LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam){staticbooldraw=false;staticboolleft=true;staticboolADDAVERAGE=true;intwmEvent;PAINTSTRUCTps;HDChdc;HBRUSHhBrush;HPENhPen;//HMENUhmenu,haddmenu;switch(message){caseWM_COMMAND:wmEvent=HIWORD(wParam);switch(LOWORD(wParam)){caseIDM_MFANGCHA:ModifyMenu(hmenu,IDM_FANGCHA,MF_BYCOMMAND,IDM_nihe,线性拟合);DrawMenuBar(hWnd);break;caseIDM_ADDAVERAGE:if(ADDAVERAGE){InsertMenu(hmenu,IDM_JUNFANGGEN,MF_BYCOMMAND|MF_ENABLED,IDM_AVERAGE,平均值);DrawMenuBar(hWnd);}ADDAVERAGE=false;break;caseIDM_ABOUT:DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),hWnd,About);break;caseIDM_DRAW:hmenu=GetMenu(hWnd);draw=true;break;caseIDM_EXIT:DestroyWindow(hWnd);break;default:returnDefWindowProc(hWnd,message,wParam,lParam);}break;caseWM_LBUTTONDOWN:if(left){hmenu=GetMenu(hWnd);haddmenu=CreateMenu();AppendMenu(haddmenu,MF_GRAYED,IDM_DZONGHE,删除计算总和);AppendMenu(haddmenu,MF_GRAYED,IDM_MFANGCHA,修改计算方差);AppendMenu(haddmenu,MF_GRAYED,IDM_ADDAVERAGE,添加计算平均值);InsertMenu(hmenu,3,MF_POPUP|MF_BYPOSITION,(UINT)haddmenu,修改菜单);DrawMenuBar(hWnd);}left=false;break;caseWM_RBUTTONDOWN:EnableMenuItem(haddmenu,IDM_DZONGHE,MF_ENABLED);EnableMenuItem(haddmenu,IDM_MFANGCHA,MF_ENABLED);EnableMenuItem(haddmenu,IDM_ADDAVERAGE,MF_ENABLED);DrawMenuBar(hWnd);break;caseWM_PAINT:if(draw){hdc=BeginPaint(hWnd,&ps);SetMapMode(hdc,MM_TEXT);Rectangle(hdc,150,50,300,200);hBrush=CreateHatchBrush(HS_CROSS,RGB(255,0,0));SelectObject(hdc,hBrush);Ellipse(hdc,150,60,300,190);hPen=CreatePen(PS_DASHDOT,1,RGB(0,255,0));SelectObject(hdc,hPen);MoveToEx(hdc,100,125,NULL);LineTo(hdc,350,125);MoveToEx(hdc,225,30,NULL);LineTo(hdc,225,220);EndPaint(hWnd,&ps);}break;caseWM_DESTROY:PostQuitMessage(0);break;default:returnDefWindowProc(hWnd,message,wParam,lParam);}return0;}INT_PTRCALLBACKAbout(HWNDhDlg,UINTmessage,WPARAMwParam,LPARAMlParam){UNREFERENCED_PARAMETER(lParam);switch(message){caseWM_INITDIALOG:return(INT_PTR)TRUE;caseWM_COMMAND:if(LOWORD(wParam)==IDOK||LOWORD(wParam)==IDCANCEL){EndDialog(hDlg,LOWORD(wParam));return(INT_PTR)TRUE;}break;}return(INT_PTR)FALSE;}Resource.h#defineIDC_MYICON2#defineIDD_FINAL_DIALOG102#defineIDS_APP_TITLE103#defineIDD_ABOUTBOX103#defineIDM_ABOUT104#defineIDM_EXIT105#defineIDC_FINAL109#defineIDR_MAINFRAME128#defineIDC_CURSOR2133#defineIDI_ICON1134#defineID_Menu32772#defineIDM_OPEN32773#defineIDM_SAVE32774#defineID_Menu3277532775#defineIDM_DRAW32777#defineID_Menu327783277

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功