我正在使用C ++ win32 API …
我有一个Windows消息框包含OKCANCEL
button…
该消息框右上方有一个closures(X-Button)…
retun1=MessageBox(hDlg,TEXT("Your password will expired,you must change the password"),TEXT("Logon Message"),MB_OK | MB_ICONINFORMATION);
我只想closures消息框使用CANCEL
button…
所以,我想禁用Xbutton图标…
我已经尝试MB_ICONMASK
MB_MODEMASK
这样的MB_MODEMASK
。
但我不能得到它,我需要…
我如何解决它?
最有可能的问题超出了你给我们的一个更大的问题,但是禁用关闭按钮的一种方法是将类样式设置为包含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 }
在你的OnInitDialog中,你可以尝试:
CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { //disable the X pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_GRAYED); }
你可以使用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; }