Désactiver l’icône X-Button en haut à droite dans Messagebox en utilisant l’API C ++ Win32?

J’utilise une API win32 C ++ …

j’ai un messagebox Windows contient le bouton OKCANCEL

le messagebox a une fermeture (bouton X) en haut à droite …

retun1=MessageBox(hDlg,TEXT("Your password will expired,you must change the password"),TEXT("Logon Message"),MB_OK | MB_ICONINFORMATION);

Je veux seulement fermer le message en utilisant le bouton CANCEL

Donc, je veux désactiver l’icône X-Button …

Je suis déjà essayer MB_ICONMASK MB_MODEMASK Somethink comme ça.

Mais je ne peux pas le comprendre, ce dont j’ai besoin …

Comment puis-je le résoudre?

Il y a très probablement un problème plus important que ce que vous nous avez donné, mais une façon de désactiver le bouton Fermer est de définir le style de classe pour qu’il inclue CS_NOCLOSE , ce que vous pouvez faire avec un handle de fenêtre et SetClassLongPtr . Prenons l’exemple complet suivant:

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

Dans votre OnInitDialog, vous pouvez essayer:

 CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { //disable the X pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_GRAYED); } 

Vous pouvez utiliser SetWindowsHookEx() pour installer un hook WH_CBT spécifique au WH_CBT pour obtenir le HWND de MessageBox, puis vous pouvez le manipuler comme vous le souhaitez. Par exemple:

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

Sur Windows Vista et versions ultérieures, il existe une autre solution: utilisez TaskDialogIndirect() au lieu de MessageBox() . L’omission de l’indicateur TASKDIALOGCONFIG.dwFlags champ TASKDIALOGCONFIG.dwFlags désactivera le bouton X, ainsi que la clé d’échappement:

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