使用C++ Win32 API禁用Messagebox右侧顶部的X按钮图标?

San*_*onu 5 c++ windows winapi dialog

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

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

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

retun1=MessageBox(hDlg,TEXT("Your password will expired,you must change the password"),TEXT("Logon Message"),MB_OK | MB_ICONINFORMATION);

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

所以,我想禁用X-Button图标......

我已经尝试过MB_ICONMASK MB_MODEMASKSomethink.

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

我该如何解决它?

mot*_*s_g 6

在OnInitDialog中,您可以尝试:

CMenu* pSysMenu = GetSystemMenu(FALSE);

if (pSysMenu != NULL)
{
//disable the X
pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_GRAYED);
} 
Run Code Online (Sandbox Code Playgroud)


chr*_*ris 3

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

#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
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,“CS_NOCLOSE”将影响*所有*消息框,因为它是类样式。 (3认同)