Results 1 to 7 of 7

Thread: Detecting that the window is closing by subclassing , Doesn't work :(

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Angry Detecting that the window is closing by subclassing , Doesn't work :(

    I have this code in a Module (warning not a safe subclass routine be careful) :
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    4.  
    5. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    6.  
    7. Public Const GWL_WNDPROC = (-4)
    8. Dim PrevProc As Long
    9. Private Const WM_CLOSE = &H10
    10.  
    11. Public Sub HookForm(FormName As Form)
    12. PrevProc = SetWindowLong(FormName.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    13. End Sub
    14.  
    15. Public Sub UnHookForm(FormName As Form)
    16. SetWindowLong FormName.hWnd, GWL_WNDPROC, PrevProc
    17. End Sub
    18.  
    19. Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    20. If uMsg = WM_CLOSE Then Beep ' <- I want my actions here but to simplify it I remove my code & added Beep.
    21. WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    22. End Function

    & this for the Form :
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. HookForm Me
    5. End Sub
    6.  
    7. Private Sub Form_Unload(Cancel As Integer)
    8. UnHookForm Me
    9. End Sub

    Detecting the WM_CLOSE message doesn't work for me , I tried WM_DESTROY = &H2 and WM_NCDESTROY = &H82 but nothing work , for testing I tried WM_SYSCOMMAND = &H112 and it work with any system command , my question is what I missed in that routine & how I can make it work????
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Detecting that the window is closing by subclassing , Doesn't work :(

    why are you using subclassing to detect if the window is closing? Just use the Query_Unload event...

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detecting that the window is closing by subclassing , Doesn't work :(

    The Form_QueryUnload event fires upon the code calling an Unload Form1 or Unload Me or from the user clicking the 'x' close button in the upper right hand corner of the form. It also receives an UnloadMode argument that can tell you one of 6 ways the form is being closed.

    vbFormControlMenu: The user chose the Close command from the Control menu on the form.
    vbFormCode: The Unload statement is invoked from code.
    vbAppWindows: The current Microsoft Windows operating environment session is ending.
    vbAppTaskManager: The Microsoft Windows Task Manager is closing the application.
    vbFormMDIForm: An MDI child form is closing because the MDI form is closing.
    vbFormOwner: A form is closing because its owner is closing.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Re: Detecting that the window is closing by subclassing , Doesn't work :(

    I know all of this but I really want this via subclassing , isn't it possible.

    Anyway thanks for replying.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Detecting that the window is closing by subclassing , Doesn't work :(

    Could it be that since you are subclassing your own app that the WM_CLOSE message is received and acted upon before it gets to your code? Perhaps this is another good reason for not subclassing your own app for this type of message.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Detecting that the window is closing by subclassing , Doesn't work :(

    Use subclassing when you need it, don't use it for the sake of subclassing.

    If you want to really get into Windows programming, create the windows entirely yourself with the appropriate API commands.

    WM_CLOSE is sent to a window as a command to close. WM_DESTROY is the message received to close the window. A snippet from one of my codes (where I handle all window processing manually in a custom WindowProc):
    VB Code:
    1. Case WM_DESTROY
    2.             ' destroy window
    3.             PostQuitMessage 0&

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Smile Re: Detecting that the window is closing by subclassing , Doesn't work :(

    @ Merri : I will try your hint , thanks.

    Quote Originally Posted by RobDog888
    Could it be that since you are subclassing your own app that the WM_CLOSE message is received and acted upon before it gets to your code? Perhaps this is another good reason for not subclassing your own app for this type of message.
    I guess this is true & this is the problem here.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

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