How can i prevent a dialog closing by Alt+F4 ?
I am using PreTranslateMessage(),,,,what is the Virtual key #define for Alt+F4 or is there any other way?,,,,thank you

Dani AI

Generated

Short answer: don’t try to “catch” the Alt+F4 key combo at the keyboard level — treat it as a normal close request and handle the close message. PreTranslateMessage is unreliable for system combos (as pointed out), and swallowing WM_SYSKEYDOWN bluntly (see ’s example) will break the whole system menu.

Recommended approaches

  • Intercept the system close and either ignore it or confirm with the user. That keeps the OS behavior intact and only cancels the actual close action when you want to.
  • In plain Win32, handle WM_SYSCOMMAND and test for SC_CLOSE:
case WM_SYSCOMMAND:
    if ((wParam & 0xFFF0) == SC_CLOSE) {
        // prompt or ignore
        if (UserConfirmedClose()) {
            return DefWindowProc(hwnd, uMsg, wParam, lParam); // allow close
        } else {
            return 0; // cancel close
        }
    }
    break;
  • In MFC dialogs override OnSysCommand or OnClose and ignore SC_CLOSE when appropriate (ties back to ’s suggestion):
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == SC_CLOSE) {
        if (AfxMessageBox("Discard changes?", MB_YESNO) == IDYES)
            CDialog::OnSysCommand(nID, lParam); // proceed
        // otherwise do nothing (cancel)
    } else {
        CDialog::OnSysCommand(nID, lParam);
    }
}

Other options and cautions

  • If you really want to remove the UI affordance, gray or delete the Close menu item via GetSystemMenu / EnableMenuItem, but that’s heavy-handed.
  • Prefer showing a confirmation when there’s unsaved work instead of permanently disabling Alt+F4 — it respects platform conventions and avoids surprising users.

Recommended Answers

All 8 Replies

It's a combination of the F4 key code combined with the alt modifiers, you need to check both.

But i'd avoid trying to prevent it if I was you....

Member Avatar for Member #248612

If I remember correctly, Alt+F4 never reaches PreTranslateMessage()

Here's the list of virtual key codes. jencas makes a good point that I was wondering about myself. Still, give it a try and let us know what happens.

How can i prevent a dialog closing by Alt+F4 ?
I am using PreTranslateMessage()

Instead of PreTranslateMessage(), add a handler for WM_SYSCOMMAND and detect SC_CLOSE.

You sure it's not WM_CLOSE?

You sure it's not WM_CLOSE?

Yes, SC_CLOSE comes with WM_SYSCOMMAND.

Seems pretty simple to me, add this to the dialog message procedure, and windows shouldn't handle the ALT-F4.

case WM_SYSKEYDOWN: break;

Seems pretty simple to me, add this to the dialog message procedure, and windows shouldn't handle the ALT-F4.

case WM_SYSKEYDOWN: break;

Hmm .. that renders the system menu completely ineffective, which is probably not what the OP wants.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.