使用 C++ Win32 API 禁用消息框右上角的 X 按钮图标?

2022-01-15 00:00:00 windows dialog winapi c++

我正在使用 C++ win32 API...

i am using C++ win32 API...

我有一个包含 OKCANCEL 按钮的 Windows 消息框...

i have a Windows messagebox contain OKCANCEL Button...

消息框的右上角有一个关闭(X-Button)...

the messagebox have a close(X-Button) on the right top...

retun1=MessageBox(hDlg,TEXT("您的密码将过期,必须更改密码"),TEXT("登录消息"),MB_OK | MB_ICONINFORMATION);

我只想使用 CANCEL 按钮关闭消息框...

i only want to close the messagebox using the CANCEL Button...

所以,我想禁用 X 按钮图标...

So,i want to disable the X-Button Icon...

我已经在尝试 MB_ICONMASK MB_MODEMASK 这样想.

i am already try MB_ICONMASK MB_MODEMASK Somethink like that.

但我无法得到它,我需要什么......

But i cant get it,what i need...

我该如何解决?

推荐答案

除了你给我们的问题之外,很可能还有一个更大的问题,但禁用关闭按钮的一种方法是将类样式设置为包含 CS_NOCLOSE,您可以使用窗口句柄和 SetClassLongPtr 来完成.考虑以下完整示例:

There's most likely a bigger problem beyond what you've given us, but one way to disable the close button is to set the class style to include CS_NOCLOSE, which you can do with a window handle and SetClassLongPtr. Consider the following full example:

#include <windows.h>

DWORD WINAPI CreateMessageBox(void *) { //threaded so we can still work with it
    MessageBox(nullptr, "Message", "Title", MB_OKCANCEL);
    return 0;
}

int main() {
    HANDLE thread = CreateThread(nullptr, 0, CreateMessageBox, nullptr, 0, nullptr);

    HWND msg;
    while (!(msg = FindWindow(nullptr, "Title"))); //The Ex version works well for you

    LONG_PTR style = GetWindowLongPtr(msg, GWL_STYLE); //get current style
    SetWindowLongPtr(msg, GWL_STYLE, style & ~WS_SYSMENU); //remove system menu 

    WaitForSingleObject(thread, INFINITE); //view the effects until you close it
}

相关文章