您可以使用SetWindowsHookEx()
安装特定于线程的WH_CBT
钩子来获取 MessageBox HWND
,然后您可以以任何您想要的方式操作它。例如:
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;
}
在 Windows Vista 及更高版本上,还有另一种解决方案 - 使用TaskDialogIndirect()
而不是MessageBox()
. 从字段中省略TDF_ALLOW_DIALOG_CANCELLATION
标志TASKDIALOGCONFIG.dwFlags
将禁用 X 按钮以及 Escape 键:
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;
}