1 #include <Windows.h> 2 3 //处理消息的回调函数 4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 5 6 int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hpreinstance, PSTR szcmdline, int icmdshow) 7 { 8 //设置窗口的名字 9 static TCHAR szappname[100] = TEXT("mywindow"); 10 11 //窗口类 12 WNDCLASS wndclass; 13 //风格 14 wndclass.style = CS_HREDRAW | CS_VREDRAW; 15 wndclass.lpszClassName = szappname;//设置名字 16 wndclass.lpszMenuName = NULL; 17 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//设置背景 18 wndclass.lpfnWndProc = WndProc;//设置回调函数处理窗口 19 wndclass.cbClsExtra = wndclass.cbWndExtra = 0;//拓展 20 wndclass.hInstance = hinstance;//窗口句柄 21 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//载入图标 22 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);//载入鼠标风格 23 24 //通过名字,注册窗口 25 if (!RegisterClass(&wndclass)) 26 { 27 MessageBoxA(0, "窗口注册失败", "提示", 0); 28 return 0; 29 } 30 31 //通过名字,创建窗口 32 HWND hwnd = CreateWindow(szappname,//窗口名字 33 TEXT("窗口"),//窗口标题 34 WS_OVERLAPPEDWINDOW,//窗口风格 35 CW_USEDEFAULT,//默认显示位置x 36 CW_USEDEFAULT,//默认显示位置y 37 CW_USEDEFAULT,//默认宽度 38 CW_USEDEFAULT,//默认高度 39 NULL,//子窗口 40 NULL,//子菜单 41 hinstance,//窗口句柄 42 NULL); 43 44 ShowWindow(hwnd, SW_SHOW); 45 UpdateWindow(hwnd);//显示并更新 46 MSG msg;//消息 47 //消息循环(GetMessage 创建事件 TranslateMessage DispatchMessage响应事件) 48 while (GetMessage(&msg,NULL,0,0)) 49 { 50 TranslateMessage(&msg); 51 //调用回调函数 52 DispatchMessage(&msg); 53 } 54 55 return msg.wParam; 56 } 57 58 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) 59 { 60 //设备环境 61 HDC hdc; 62 PAINTSTRUCT ps; 63 RECT rect; 64 switch (message) 65 { 66 case WM_LBUTTONDOWN: 67 MessageBoxA(0, "按下鼠标左键", "提示", 0); 68 break; 69 case WM_RBUTTONDOWN: 70 MessageBoxA(0, "按下鼠标左键", "提示", 0); 71 break; 72 case WM_CLOSE: 73 if (MessageBoxA(0, "是否关闭窗口", "提示", 1) == 1) 74 { 75 //关闭窗口,发送WM_DESTROY消息 76 DestroyWindow(hwnd); 77 } 78 else 79 { 80 return 0; 81 } 82 break; 83 case WM_DESTROY: 84 MessageBoxA(0, "已成功关闭", "提示", 0); 85 exit(0); 86 break; 87 default: 88 break; 89 } 90 return DefWindowProc(hwnd, message, wparam, lparam); 91 }