实现过程
- 设计窗口
- 注册窗口
- 创建窗口
- 显示和更新窗口
- 通过循环取消息
- 处理消息(窗口过程)
#include <windows.h>
LRESULT CALLBACK WindowsProc1(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
) {
switch (Msg)
{
case WM_CLOSE:
{
DestroyWindow(hWnd);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
char buf[1024];
wsprintf(buf,TEXT("坐标X=%d,坐标Y=%d"), x, y);
MessageBox(hWnd, buf, TEXT("鼠标左键按下"), 0x00000000L);
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 300, 300, TEXT("HELLO"), strlen("HELLO"));
EndPaint(hdc, &ps);
break;
}
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
};
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_HAND);
wc.hIcon = LoadIcon(NULL, IDI_ERROR);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowsProc1;
wc.lpszClassName = TEXT("WIN98");
wc.lpszMenuName = NULL;
wc.style = 0;
RegisterClass(&wc);
HWND hwnd= CreateWindow(wc.lpszClassName, TEXT("WINDOWS"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (1)
{
if (GetMessage(&msg, NULL, 0, 0)==FALSE)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}