实验描述:
1 . Create a menu which includes three menu items: File, Calculate and
Help.
2 . When the Draw(G) menu item of the File(F) menu is clicked, the
figure is drawn as shown below.
3 . And when the user left-click in the client area of the window, another
menu called Modify which includes three sub-menu items appears. And this three
sub-menu items will not be made enabled until the user right-click in the
client area of the window.
left-click the mouse:
right-click the mouse:
4 . And when the sub-menu Modify Mean Square Error of the Modify menu is
clicked, the third sub-menu of Calculate will change to Linear Fit as shown
below.
before clicking:
after clicking:
5 . And when the sub-menu Add Average of the Modify menu is clicked, a
new sub-menu called average will be inserted to the menu Calculate.
after clicking:
6 . Make the cursor and icon of the program to any one you like which
should be different.
源代码如下:
.cpp文件:
#include<windows.h>
#include"resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HMENU hMenu;
HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
static TCHAR szAppName[] = TEXT("Experiment2");
MSG msg;
WNDCLASS wndclass;
hInst = hInstance;
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wndclass.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Program require Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hMenu = LoadMenu(hInstance, "MENU_1");
hwnd = CreateWindow(
szAppName,
TEXT("Experiment2"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
hMenu,
hInstance,
NULL
);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu2 = LoadMenu(hInst, "MENU_2");
HDC hdc;
static bool DRAW = FALSE;
HBRUSH hBrush;
HPEN hPen;
switch(message)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_DRAW:
DRAW = TRUE;
InvalidateRect(hWnd, NULL, TRUE);
break;
case IDM_MODIFY_MEAN_SQUARE_ERROR:
ModifyMenu(GetSubMenu(hMenu, 1), IDM_ROOT_MEAN_SQUARE, MF_BYCOMMAND, IDM_ROOT_MEAN_SQUARE, "Linear Fit");
DrawMenuBar(hWnd);
break;
case IDM_ADD_AVERAGE:
InsertMenu(GetSubMenu(hMenu, 1), 1, MF_BYPOSITION, MF_ENABLED, "Average");
DrawMenuBar(hWnd);
break;
}
break;
case WM_LBUTTONDOWN:
hMenu = GetMenu(hWnd);
DeleteMenu(hMenu, 3, MF_BYPOSITION);
InsertMenu(hMenu, 3, MF_POPUP, (UINT)hMenu2, "Modify");
DrawMenuBar(hwnd);
break;
case WM_RBUTTONDOWN:
EnableMenuItem(hMenu, IDM_DELETE_SUMMARY, MF_ENABLED);
EnableMenuItem(hMenu, IDM_ADD_AVERAGE, MF_ENABLED);
EnableMenuItem(hMenu, IDM_MODIFY_MEAN_SQUARE_ERROR, MF_ENABLED);
DrawMenuBar(hWnd);
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
hBrush = CreateHatchBrush(HS_CROSS, RGB(255, 0, 0));
hPen = CreatePen(PS_DASHDOT, 1, RGB(0, 255, 0));
if(DRAW == TRUE)
{
Rectangle(hdc, 100, 50, 200, 150);
SelectObject(hdc, hBrush);
Ellipse(hdc, 100, 50, 200, 150);
SelectObject(hdc, hPen);
MoveToEx(hdc, 50, 100, NULL);
LineTo(hdc, 250, 100);
MoveToEx(hdc, 150, 0, NULL);
LineTo(hdc, 150, 200);
DeleteObject(hBrush);
DeleteObject(hPen);
}
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
.rc文件:
“MENU_1”
“MENU_2”
结果图片与实验要求图片同!