实验描述:
Design a program to realize the application of menu and dialog box.
源代码如下:
#include <windows.h>
#define IDE_SHOW 301
#define IDE_HIDE 302
#define IDE_EXIT 303
HMENU hMenu =CreateMenu();
HWND hwnd1;
HMENU hMPop;
HINSTANCE hInst;
LRESULT CALLBACK WndProc1(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wndclassex;
HWND hwnd;
MSG msg;
WNDCLASS Wndclass;
memset(&wndclassex,0,sizeof(wndclassex));
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.lpfnWndProc = WndProc;
wndclassex.hInstance = hInstance;
wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassex.lpszClassName = "WindowClass";
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
static TCHAR szAppName[] = TEXT("classname");
Wndclass.style = CS_HREDRAW | CS_VREDRAW;
Wndclass.lpfnWndProc = WndProc1;
Wndclass.cbClsExtra = 0;
Wndclass.cbWndExtra = sizeof(long);
Wndclass.hInstance = hInstance;
Wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
Wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
Wndclass.lpszMenuName = NULL;
Wndclass.lpszClassName = szAppName;
if(!RegisterClassEx(&wndclassex))
{
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
if(!RegisterClass(&Wndclass))
{
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"WindowClass",
"Dialog Demo",
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, hMenu, hInstance, NULL
);
hwnd1 = CreateWindow(
szAppName,
NULL,
WS_DLGFRAME ,
200,
160,
200,
100,
hwnd, NULL,
hInstance,NULL
);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc1(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
HBRUSH hBrush;
RECT rect ;
PAINTSTRUCT ps;
HDC hdc;
switch(Message)
{
case WM_PAINT:
hBrush = CreateSolidBrush(RGB(230, 230, 230));
hdc = BeginPaint(hwnd, &ps);
SetBkColor(hdc, RGB(230,230,230));
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, hBrush);
DrawText(hdc, TEXT("Let's learn VC++ together!"), -1, &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, Message, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
hMPop = CreateMenu();
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMPop, TEXT("File(F)"));
AppendMenu(hMPop, MF_STRING, IDE_SHOW, TEXT("Show"));
AppendMenu(hMPop, MF_STRING, IDE_HIDE, TEXT("Hide"));
AppendMenu(hMPop, MF_SEPARATOR, 0, 0);
AppendMenu(hMPop, MF_STRING, IDE_EXIT, TEXT("Exit"));
SetMenu(hwnd, hMenu);
SetMenu(hwnd, hMPop);
break;
case WM_COMMAND :
switch(LOWORD(wParam))
{
case IDE_SHOW:
ShowWindow(hwnd1,SW_SHOW);
UpdateWindow(hwnd1);
break;
case IDE_HIDE:
ShowWindow(hwnd1,SW_HIDE);
UpdateWindow(hwnd1);
break;
case IDE_EXIT:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, Message, wParam, lParam);
}
结果如下图:
点击 File(F):