C++开发原生WIN32程序
VS2019 文件-新建-项目-Windows桌面向导(C++)-桌面应用程序 空项目
项目属性-高级-字符集未设置
程序内所有字符串用TEXT宏包裹
1 #include <windows.h>
2
3 LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
4
5 int WINAPI WinMain(
6 _In_ HINSTANCE hInstance,
7 _In_opt_ HINSTANCE hPrevInstance,
8 _In_ LPSTR lpCmdLine,
9 _In_ int nShowCmd)
10 {
11 WNDCLASS wc;
12 HWND hwnd;
13 MSG msg;
14
15 wc.style = 0;
16 wc.lpfnWndProc = (WNDPROC)WndProc;
17 wc.cbClsExtra = 0;
18 wc.cbWndExtra = 0;
19 wc.hInstance = hInstance;
20 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
21 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
22 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
23 wc.lpszMenuName = NULL;
24 wc.lpszClassName = "MyWndClass";
25
26 RegisterClass(&wc);
27 hwnd = CreateWindow("MyWndClass", "SDK Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
28 ShowWindow(hwnd, nShowCmd);
29 UpdateWindow(hwnd);
30
31 while (GetMessage(&msg, NULL, 0, 0))
32 {
33 TranslateMessage(&msg);
34 DispatchMessage(&msg);
35 }
36 return msg.wParam;
37 }
38
39 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
40 {
41 PAINTSTRUCT ps;
42 HDC hdc;
43 switch (message)
44 {
45 case WM_PAINT:
46 hdc = BeginPaint(hwnd, &ps);
47 Ellipse(hdc, 0, 0, 200, 100);
48 EndPaint(hwnd, &ps);
49 return 0;
50 case WM_DESTROY:
51 PostQuitMessage(0);
52 return 0;
53 }
54 return DefWindowProc(hwnd, message, wParam, lParam);
55 }