为对话框启用关闭按钮 c++ winapi
Posted
技术标签:
【中文标题】为对话框启用关闭按钮 c++ winapi【英文标题】:Enable Close Button for Dialog c++ winapi 【发布时间】:2019-07-29 12:32:42 【问题描述】:有简单的窗口,打开对话框的按钮。但我想为其添加关闭按钮。
我尝试通过添加 WM_SYSMENU 添加关闭按钮,添加 EnableMenuItem,但它不起作用:
最少的代码是: 资源.rc:
IDD_FORMVIEW DIALOGEX 0, 0, 178, 86
STYLE DS_SETFONT | DS_CONTROL | WS_VISIBLE | WS_SYSMENU
EXSTYLE WS_EX_OVERLAPPEDWINDOW
CAPTION "TEST"
FONT 12, "Microsoft Sans Serif", 400, 0, 0xCC
BEGIN
CTEXT "Test",IDC_STATIC,46,4,74,8,0,WS_EX_CLIENTEDGE
END
resource.h:
#define IDD_FORMVIEW 101
Test.cpp:
hwndButton = CreateWindowW(L"BUTTON",
L"ENTER",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS,
605, 510, 255, 45,
hWnd, (HMENU)1, NULL, NULL);
case WM_COMMAND:
switch (LOWORD(wParam))
case 2:
if (hwndHTDW == NULL)
hwndHTDW = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_FORMVIEW), hWnd, DialogProc);
ShowWindow(hwndHTDW, SW_SHOW);
UpdateWindow(hwndHTDW);
break;
switch (msg)
case WM_INITDIALOG:
EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,
MF_BYCOMMAND | MF_ENABLED);
return TRUE;
break;
default:
return FALSE;
return TRUE;
【问题讨论】:
关闭对话框的规范方法是“确定”按钮。有关示例,请参见 docs.microsoft.com/en-us/windows/win32/dlgbox/…。 所以我可以创建一个按钮来关闭它,对吧。使用 EndDialog 请正确格式化您的代码。 是的,使用EndDialog
并将对话框的句柄作为参数输入。
【参考方案1】:
稍微修改一下你的代码。
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
hwndButton = CreateWindowW(L"BUTTON",
L"ENTER",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS,
605, 510, 255, 45,
hWnd, (HMENU)1, NULL, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
case WM_COMMAND:
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case 1:
if (hwndHTDW == NULL)
hwndHTDW = CreateDialog(hInst, MAKEINTRESOURCE(IDD_FORMVIEW), hWnd, DialogProc);
Button_Close = CreateWindowW(L"BUTTON",
L"CLOSE",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS | WS_TABSTOP,
130, 100, 80, 30,
hwndHTDW, (HMENU)2, NULL, NULL);
ShowWindow(hwndHTDW, SW_SHOW);
UpdateWindow(hwndHTDW);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
return 0;
BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
BOOL fError;
switch (message)
case WM_INITDIALOG:
EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,
MF_BYCOMMAND | MF_ENABLED);
return TRUE;
break;
case WM_COMMAND:
switch (LOWORD(wParam))
case 2:
EndDialog(hwnd, LOWORD(wParam));
return (INT_PTR)TRUE;
default:
break;
default:
return FALSE;
return TRUE;
更新:
通过CreateWindow
创建一个额外的按钮,并将对话框的句柄作为参数传递给按钮。
HWND Button_Close = CreateWindowW(L"BUTTON",
L"CLOSE",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS | WS_TABSTOP,
130, 100, 80, 30,
hwndHTDW, (HMENU)2, NULL, NULL);
在对话框的回调函数中,添加WM_COMMAND
事件并添加如下代码。
case WM_COMMAND:
switch (LOWORD(wParam))
case 2:
EndDialog(hwnd, LOWORD(wParam));
return (INT_PTR)TRUE;
【讨论】:
答案至少需要解释,问题是什么,以及为解决它做了什么。无论有多少漂亮(烦人)的动画 GIF,纯代码的答案都没有用。 @IInspectable 好的,我已经添加了一些解释。感谢您的提醒。以上是关于为对话框启用关闭按钮 c++ winapi的主要内容,如果未能解决你的问题,请参考以下文章