[toc]

1.在main函数中创建Button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <windows.h>

int main(int argc, char* argv[]) {

HWND hwnd = GetConsoleWindow();
//Create a Button
HWND hwndButton = CreateWindowW(
L"BUTTON", // Predefined class; Unicode assumed
L"OK", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
10, // y position
100, // Button width
100, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL); // Pointer not needed.

MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
};

2在main函数中创建对话框

  • 在项目中添加Dialog资源后编写代码。

1.用DialogBoxW函数创建对话框

1
2
3
4
5
6
7
8
9
10
11

#include <windows.h>
#include "Resource.h"

int main(int argc, char* argv[])
{
//Eject a Dialog
DialogBoxW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), NULL,NULL);
return 0;
}

2.用CreateDialogW函数创建对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <windows.h>
#include "Resource.h"

int main(int argc, char* argv[])
{
HINSTANCE hInstance = GetModuleHandle(NULL);
//Eject a Dialog
HWND hdlg = CreateDialogW(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);
ShowWindow(hdlg, SW_SHOW);
UpdateWindow(hdlg);
system("pause");
return 0;
}

3在winamin函数中创建对话框

1.在windows桌面程序中微软将程序的入口点更改为了WinMain。

  1. 更改入口点函数有两种方式,第一种是通过预处理指令更改。Debug模式下仍然会有控制台显示,Release版本不显示控制台
1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include "resource.h"
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"WinMainCRTStartup\"" ) // 设置入口地址

int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
DialogBoxW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, NULL);
return 0;

}
  1. 第二种是在VistualStudio2022中将项目属性->Links->system->SubSystem更改为windows
1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include "resource.h"

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

DialogBoxW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, NULL);
return 0;

}

2. 添加回调函数处理对话框事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <Windows.h>
#include "resource.h"

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hThisApp, HINSTANCE hPrevApp, LPSTR lpCmd, int nShow)
{
HWND hdlg = CreateDialog(hThisApp, MAKEINTRESOURCE(IDD_DIALOG1), GetDesktopWindow(), (DLGPROC)DlgProc);
if (!hdlg)
{
return 0;
}
ShowWindow(hdlg, SW_SHOW);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return 0;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
PostQuitMessage(0);//退出
}
return 0;
}
return (INT_PTR)FALSE;
}

3 加载菜单

在项目中添加资源文件里的菜单后编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <Windows.h>
#include "resource.h"

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hThisApp, HINSTANCE hPrevApp, LPSTR lpCmd, int nShow)
{
HWND hdlg = CreateDialog(hThisApp, MAKEINTRESOURCE(IDD_DIALOG1), GetDesktopWindow(), (DLGPROC)DlgProc);
if (!hdlg)
{
return 0;
}
ShowWindow(hdlg, SW_SHOW);
//Load Menu
HMENU hmenu = LoadMenuW(hThisApp, MAKEINTRESOURCEW(IDR_MENU1));
//Set Menu
SetMenu(hdlg, hmenu);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return 0;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
PostQuitMessage(0);//退出
}
return 0;
}
return (INT_PTR)FALSE;
}

用IDD_FORMVIEW资源作为主窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <Windows.h>
#include "resource.h"

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam);

HINSTANCE hgAppInst;
int WINAPI WinMain(HINSTANCE hThisApp, HINSTANCE hPrevApp, LPSTR lpCmd, int nShow)
{
// 设计窗口类
WNDCLASS wc = {0};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.lpszClassName = L"ClassName";
wc.hInstance = hThisApp;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
hgAppInst = hThisApp;
// 创建窗口
HWND hwnd = CreateWindow(L"ClassName", L"title",
WS_OVERLAPPEDWINDOW, 40, 25, 380, 300, NULL, NULL, hThisApp, NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd, nShow);
UpdateWindow(hwnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
// 创建对话框
HWND hdlg = CreateDialog(hgAppInst, MAKEINTRESOURCE(IDD_FORMVIEW), hwnd, (DLGPROC)DlgProc);
// 显示对话框
ShowWindow(hdlg, SW_SHOWNA);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_SIZE:
{
//获取可用桌面大小
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
// 获取控件句柄
HWND hdlg = GetDlgItem(hwnd, IDD_FORMVIEW);
MoveWindow(hdlg,0,0, rect.right-rect.left,rect.top-rect.bottom,FALSE);
break;
}
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_F1:
MessageBoxW(hwnd, L"F1键按下", L"提示", MB_OK);
break;
case VK_F2:
MessageBoxW(hwnd, L"F1键按下", L"提示", MB_OK);
break;
}
return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

// 处理对话框消息
INT_PTR CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return 0;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
PostQuitMessage(0);//退出
}
return 0;
case WM_COMMAND:
switch (wParam)
{
case IDC_BUTTON1:
MessageBox(0, 0, 0, 0);
break;
}
return (INT_PTR)TRUE;

}
return (INT_PTR)FALSE;
}