Results 1 to 10 of 10
  1. #1
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    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.

  3. #3
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Bruce,

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

    Thanks,
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  5. #5
    2 Star Lounger
    Join Date
    Nov 2001
    Location
    London, UK, England
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Check out http://www.vbaccelerator.com 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...

  6. #6
    New Lounger
    Join Date
    Jun 2003
    Location
    Australind, Western Australia, Australia
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    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.

  8. #8
    New Lounger
    Join Date
    Jun 2003
    Location
    Australind, Western Australia, Australia
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

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

    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!

  10. #10
    New Lounger
    Join Date
    Jun 2003
    Location
    Australind, Western Australia, Australia
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Cheers Hans. I forgot to add a smiley to my first remark, so I hope it came across as intended and not <img src=/S/cranky.gif border=0 alt=cranky width=18 height=25>

    Found the smilies.

Posting Permissions

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