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

后端 未结 3 873
一向
一向 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:52

    You can use SetWindowsHookEx() to install a thread-specific WH_CBT hook to obtain the MessageBox's HWND, then you can manipulate it any way you want. For example:

    HHOOK hHook = NULL;
    
    LRESULT CALLBACK CBTHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode == HCBT_CREATEWND)
        {
            HWND hMsgBox = (HWND) wParam;
            LONG_PTR style = GetWindowLongPtr(hMsgBox, GWL_STYLE);
            SetWindowLongPtr(hMsgBox, GWL_STYLE, style & ~WS_SYSMENU);
        }
    
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    }
    
    int WarnAboutPasswordChange(HWND hDlg)
    {
        hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTHookProc, NULL, GetCurrentThreadId());
    
        int retun1 = MessageBox(hDlg, TEXT("Your password will expired, you must change the password"), TEXT("Logon Message"), MB_OK | MB_ICONINFORMATION);
    
        if (hHook)
        {
            UnhookWindowsHookEx(hHook);
            hHook = NULL;
        }
    
        return retun1;
    }
    

    On Windows Vista and later, there is another solution - use TaskDialogIndirect() instead of MessageBox(). Omitting the TDF_ALLOW_DIALOG_CANCELLATION flag from the TASKDIALOGCONFIG.dwFlags field will disable the X button, as well as the Escape key:

    int WarnAboutPasswordChange(HWND hDlg)
    {
        TASKDIALOGCONFIG config = {0};
        config.cbSize = sizeof(config);
        config.hwndParent = hDlg;
        config.dwCommonButtons = TDCBF_OK_BUTTON;
        config.pszWindowTitle = L"Logon Message";
        config.pszMainInstruction = L"Your password will expired, you must change the password";
        config.pszMainIcon = TD_INFORMATION_ICON;
        config.nDefaultButton = IDOK;
    
        int retun1 = 0;
        TaskDialogIndirect(&config, &retun1, NULL, NULL);
    
        return retun1;
    }
    

提交回复
热议问题