How do I know who is closing the form.

D

Dinesh Bajaj

Hi,

I am developing a multi-form PPC application in VS2003, and am looking
for a way to distinguish form close done by the user from a close done
by OS.

In VB6, we could easily make this distinction by checking the
UnloadMode event argument in the QueryUnload event. But, its equivalent
seems to be missing in .Net. So what is the way out?

Thank you for your help.

Regards,
Dinesh
 
D

Dinesh Bajaj

I learned from some similar posts in the net that we should listen to
the native windows messages generated for the form, and catch the close
message there. So, accordingly I set out for a solution and implemented
IMessageFilter interface in the form. Currently, the PreFilterMessage
in the form looks like this:

Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_CLOSE As Integer = &HF060
Private Const WM_CLOSE As Integer = &H10

Public Function PreFilterMessage(ByRef m As _
Microsoft.WindowsCE.Forms.Message) _
As Boolean Implements _
OpenNETCF.Windows.Forms.IMessageFilter.PreFilterMessage

Try
If m.HWnd.Equals(pHWnd) Then
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() =
SC_CLOSE Then
MessageBox.Show("User is closing the form")
ElseIf m.Msg = WM_CLOSE Then
MessageBox.Show("Form is closing.")
End If
End If
Catch ex As Exception
'MsgBox(ex.ToString)
End Try

End Function

Though, the code is working, and I get WM_CLOSE message each time the
form is closed by the user or by the task manager, but I am not able to
determine who is closing the form : the user or the task manager. What
message(s) should be trapped to know that the task manager is closing
the form.

Please Help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top