|
-
Jan 1st, 2007, 08:35 PM
#1
Thread Starter
Addicted Member
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:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Public Const GWL_WNDPROC = (-4)
Dim PrevProc As Long
Private Const WM_CLOSE = &H10
Public Sub HookForm(FormName As Form)
PrevProc = SetWindowLong(FormName.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHookForm(FormName As Form)
SetWindowLong FormName.hWnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CLOSE Then Beep ' <- I want my actions here but to simplify it I remove my code & added Beep.
WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
End Function
& this for the Form :
VB Code:
Option Explicit
Private Sub Form_Load()
HookForm Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHookForm Me
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????
-
Jan 1st, 2007, 08:55 PM
#2
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...
-
Jan 1st, 2007, 10:21 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 2nd, 2007, 08:29 AM
#4
Thread Starter
Addicted Member
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.
-
Jan 3rd, 2007, 02:13 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 3rd, 2007, 02:30 AM
#6
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:
Case WM_DESTROY
' destroy window
PostQuitMessage 0&
-
Jan 3rd, 2007, 05:29 AM
#7
Thread Starter
Addicted Member
Re: Detecting that the window is closing by subclassing , Doesn't work :(
@ Merri : I will try your hint , thanks.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|