int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTab;
LoadString(hInstance, IDS_SZCLASS, szClass, MAX_LOADSTRING); //加载资源里的字符串
LoadString(hInstance, IDS_SZTITLE, szTitle, MAX_LOADSTRING); //加载资源里的字符串
MyRegisterClass(hInstance);
if(!InitInstance(hInstance, SW_SHOWNORMAL))
{
return FALSE;
}
hAccelTab = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN));
while (GetMessage(&msg, NULL, 0, 0)) //获取所有窗口的消息
{
TranslateMessage(&msg); //调用函数TranslateMessage作消息转换工作
DispatchMessage(&msg); //调用函数DispatchMessage发送消息
}
return msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hinst = hInstance;
HWND hwnd;
hwnd = CreateWindow(szClass, szTitle , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return FALSE;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = MyProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_MAIN));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN);
wcex.lpszClassName = szClass;
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
LRESULT CALLBACK MyProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(uMsg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_ABOUT:
DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, DialogProc); //显示对话框
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd); //当收到退出命令的消息,就调用函数DestroyWindow,然后它发出消息WM_DESTROY
break;
case WM_DESTROY:
PostQuitMessage(0); //调用函数PostQuitMessage来处理退出应用程序
break;
case WM_PAINT:
RECT rect;
GetClientRect(hwnd,&rect);
hdc = BeginPaint(hwnd,&ps); //调用函数BeginPaint
DrawText(hdc,TEXT("Hell Word"),-1, &rect, DT_VCENTER | DT_TOP);
EndPaint(hwnd,&ps); //调用函数EndPaint
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
// 处理对话框消息
INT_PTR CALLBACK DialogProc(
HWND hwndDlg, // handle to dialog box
UINT uMsg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
UNREFERENCED_PARAMETER(lParam);
switch(uMsg)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hwndDlg,LOWORD(wParam)); //用EndDialog关闭对话框
return TRUE;
}
break;
default:
return FALSE;
}
}