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????
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...
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.
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.
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.
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&
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.