[英]Disable X-Button Icon on the top of the right in Messagebox Using C++ Win32 API?
我正在使用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_MODEMASK
這樣的MB_MODEMASK
。
但是我無法得到它,我需要什么......
我該如何解決它?
在OnInitDialog中,您可以嘗試:
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
//disable the X
pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_GRAYED);
}
除了你給我們之外,最有可能出現更大的問題,但是禁用關閉按鈕的一種方法是將類樣式設置為包含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
}
您可以使用SetWindowsHookEx()
來安裝特定WH_CBT
線程的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()
。 從TASKDIALOGCONFIG.dwFlags
字段中省略TDF_ALLOW_DIALOG_CANCELLATION
標志將禁用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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.