#include<windows.h> #include<stdio.h>
LPCSTR classname = "MY_C"; LPCSTR msgname = "first_window"; LRESULT CALLBACK my_callback( __in HWND hWindow, __in UINT uMsg, __in WPARAM wParam, __in LPARAM lParam);
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { WNDCLASS wndcls; wndcls.cbClsExtra = NULL; wndcls.cbWndExtra = NULL; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndcls.hInstance = hInstance; wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; wndcls.lpfnWndProc = my_callback; wndcls.lpszClassName = classname;
RegisterClass(&wndcls); HWND hwnd; hwnd = CreateWindow( classname, msgname, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, (HWND)NULL, (HMENU)NULL, hInstance, (LPVOID)NULL);
ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
MSG msg; while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
LRESULT CALLBACK my_callback( __in HWND hwnd, __in UINT uMsg, __in WPARAM wParam, __in LPARAM lParam) { int ret; HDC hdc; switch (uMsg) { case WM_CHAR: char szchar[20]; sprintf_s(szchar, "您刚刚按下了:%c", wParam); MessageBox(hwnd, szchar, "char", NULL); break; case WM_LBUTTONDOWN: MessageBox(hwnd, "检测鼠标左键按下", "mes", NULL); break; case WM_PAINT: PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 0, 0, "the window has repainted", strlen("the window has repainted")); EndPaint(hwnd, &ps); MessageBox(hwnd, "重绘", "mes", NULL); break; case WM_CLOSE: ret = MessageBox(hwnd, "是否真的结束", "msg", MB_YESNO); if (ret == IDYES) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); break; }
return 0; }
|