• How can I trap the close button(X) click? (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » How can I trap the close button(X) click? (VB6)

    Author
    Topic
    #388812

    Hello,
    I am looking for a way to trap the specific event when a user clicks the ‘X’ (close button) on the upper right of the form.
    I know about the Sub Form_qureryUnload(Cancel as Integer, UnloadMode as Integer) , however this returns the same UnloadMode of 0(zero) for;
    1. clicking ‘Close’ in the control menu.
    2. Alt-F4
    3. Clicking the ‘X’ in the upper right corner.
    which is how its suppose to work.

    However I want to trap for the “X” click *only* and leave the other two close methods as is.
    Any ideas on how to determine a click specifically on the ‘X’?

    I am subclassing the form to add items to the form’s control menu and also to send the App. to the system tray.
    I found that 61536 is the system command for the close event but as with the above Sub, it reports this same number for any of the 3 events listed.
    I’m sure the wHandle of the “X” can be ID’d and determine if its been click but I can’t figure out how.

    I’ve tried some heavy web searches and couldn’t find this exact answer I need.
    I have seen plenty of examples of disabling and/or removing the ‘X’. (Not what I want to do).
    I want to leave the “X” on the form, and leave it enabled so when a user clicks it, instead of closing the form it will send my App to the SysTray. If the user clicks ‘Close’ on the control menu or Alt-F4 then the App should close as normal.
    I already have the Systray code working just fine, I just need to know how to trap the ‘X’ click.

    Thanks in advance,
    Bruce

    Viewing 2 reply threads
    Author
    Replies
    • #684251

      Others may prove me wrong, but I think that the “x” button, Alt+F4 and the “Close” item in the System menu are linked at a low level. If you found the examples that enable/disable the “x” button using API functions, you will have noticed that the code enables/disables all three simultaneously. You’d probably have to trap the routine that handles the title bar, but I have no idea if that is possible and if so, how.

      • #684333

        Hans,
        Thanks for the kick in the bottom to get me out of the System Commands ‘sandbox’ and over to the Titlebar ‘sandbox’.
        I did some searches for ‘windows message’ and ‘WM_” which lead me to some links on ‘detecting a click on the caption bar’ that I was able to use.

        With my subclassed form, I found that I could trap for the WM_NCLBUTTONDOWN message and then check if wParam = HTCLOSE

        The ‘non-client left button down’ message combined with the ‘hit close’ let me ‘know’ that the user clicked the “X” close on the form, which allows me to have my code send the Application to the System Tray instead of closing it.

        I don’t fully understand the ‘non-client window’ stuff. Seems I can get the leftmousedown but not theleftmouseup.
        I would prefer if I could trap the LeftMouseUp message but it seems that windows passes the buttonup message (maybe ) to the close button for processing.? Its a minor issue but I think the user would feel warm and fuzzy if when clicking the ‘X’ they see the ‘button pressed’ just before the application goes to the system tray as oppose to, as soon as the left mouse is down, the application go to the system tray.

        What I have, works, but if anybody has any caviats about what I’ve done or knows how to trap the mouseUp on the ‘X’ , please chime in.

        Thanks!
        bruce

        • #684528

          Bruce,

          Could you post a small sample form that show how to accompish trapping clicking on the X. It sounds quite interesting.

          Thanks,

          • #685049

            Check out http://www.vbaccelerator.com[/url%5D for some excellent code & articles on Non-client windows stuff. Steve has written a ‘Neo Caption’ component that skins windows and repaints all the NC areas of forms, some good code & examples to get your head round this complex area…

    • #685956

      Hi Bruce

      I’m assuming you have your own code to close out of Excel? if so, all you need to do is Dimmension a Public Boolean Variable and then use the code below in the Privtate Module of the Workbook Object (ThisWorkbook).

      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Cancel = bClose
      End Sub

      Then In your code to close the Application use

      bClose=True

      • #685966

        Dave,

        As Bruce indicates in the subject line, his question is about the Visual Basic compiler (VB6), not about Excel. And he wants to distinguish the “x” Close button in the upper right corner of a window from the Close item in the Control menu in the upper left corner and the Alt+F4 keyboard shortcut.

    • #685969

      Silly me, I’ll never show my face in public again!

      Regardless, another that searches may find it helpful. It’s all about knowledge sharing, if a solution doesn’t fit, it wont get used.

      Bruce, you will probably get your answer from our forum here: http://www.ozgrid.com/forum/index.php Do a search for API. good luck.

      • #685972

        I agree, your tip may be just the ticket for somebody else. I just wanted to point out that it didn’t apply to the question at hand.

        Anyway, your contributions are welcome!

        • #685973

          Cheers Hans. I forgot to add a smiley to my first remark, so I hope it came across as intended and not cranky

          Found the smilies.

    Viewing 2 reply threads
    Reply To: How can I trap the close button(X) click? (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: