CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    disabling the destroy button

    Hi,

    I have a modeless dialog. While it is opened I want to prevent the user from being able to close the calling application using the destroy button.
    How do I do that?

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    You could handle the WM_CLOSE message (handles the small X on the top right of the title bar).

    In that handler, check if the modeless dialog exists, and if so, do nothing, otherwise allow the close to happen.

    A side benefit is that this also handles the ALT-F4 or even the end application from the task manager!

    Good luck.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  3. #3
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    Disabling the close button:
    EnableMenuItem (GetSystemMenu(hwnd, FALSE), SC_CLOSE,MF_BYCOMMAND | MF_GRAYED);
    Good luck

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    One disadvantage of using the EnableMenuItem is that this won't stop closing the app from the ALT-F4 or the task manager.

    Additionally, by overriding the WM_CLOSE (OnClose) message, you can display a dialog telling the user why he can't close or anything else you want.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  5. #5
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    Yes.

    If we're talking about MFC he can override the OnCancel method.
    But you cannot block all ways to close the window.
    Good luck

  6. #6
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    I'm not referring to the OnCancel message - that only handles the button and the escape key.

    If you override the WM_CLOSE message, that traps the X in the title bar, exit from the system menu, ALT--F4, and the end task from the task manager.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  7. #7
    Join Date
    Nov 2002
    Location
    Israel
    Posts
    182
    I don't know, just never cheked it, so you say that the task manager sends WM_CLOSE to applications?
    Okay. From my point of view that's good way. I even cannot believe.
    I know about SC_CLOSE because it looks strange when the close button is enabled but doesn't work. Sometimes the application cannot be closed for some reason and no message box cannot be shown (when you're drawing something huge, for example)
    Good luck

  8. #8
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772
    Thanks alot for you solutions. I think the WM_CLOSE is the solution. I had done it in DestroyWindow but this wasn't such a good idea...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured