Disable X-Button Icon on the top of the right in Messagebox Using C++ Win32 API?

后端 未结 3 879
一向
一向 2020-12-15 13:30

i am using C++ win32 API...

i have a Windows messagebox contain OKCANCEL Button...

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

3条回答
  •  孤城傲影
    2020-12-15 13:33

    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 
    
    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
    }
    

提交回复
热议问题