I have written a Custom Message Filter. And want to trace the Form_Load or
similar Message and write this to console. Can Anybody help me here.
Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As
Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
If m.Msg <> ??? Then Console.WriteLine("Form Loaded")
End Sub
Its urgent..
- Saurabh
Hi Saurabh,
I checked to see all messages that were being passed to IMessageFilter,
it seems that when we just create anew form and load it, only two
windows messages get passes to the filter.
One is a custom string message registered by the application, on my
system it had a number 0xC1FE
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/Win
dowsUserInterface/Windowing/MessagesandMessageQueues/MessagesandMessageQueuesRef
erence/MessagesandMessageQueuesMessages/WM_USER.asp),
, note that messages in the range 0xC000 - 0xFFFF are string messages
for use by apps, i guess this is used by the framework somehow, the
second message was 0x000F which is WM_PAINT.
Now the question arises, how does the Form_Load event get fired. Well a
little investigating with Reflector showed that, It is getting called
from the Application.Run() method, specfically from the
Application.RunMessageLoopInner() method
this is the relevant code
//From Application.RunMessageLoopInner()
if (reason == -1)
{
if (this.messageLoopCount != 1)
{
throw new
InvalidOperationException(SR.GetString("CantNestMessageLoops"));
}
this.applicationContext = context;
this.applicationContext.ThreadExit += new
EventHandler(this.OnAppThreadExit);
if (this.applicationContext.MainForm != null)
{
this.applicationContext.MainForm.Visible = true;
}
}
In the above code when MainForm.Visible is set to true, at that time the
overriden Form.SetVisibleCore() method gets called
//From Form.SetVisibleCore
if (this.calledCreateControl && !this.calledOnLoad)
{
this.calledOnLoad = true;
this.OnLoad(EventArgs.Empty);
if (this.dialogResult != DialogResult.None)
{
value = false;
}
}
So it maintains a flag to check if OnLoad has been called or not, and
the first time the form becomes visible the event gets fired, there is
no windows message involved.
In fact i even tried loading another form from the main form just in
case this processing was special for the main form, but still i got the
same results, only 1 window message (WM_PAINT) was fired for the second
form.
Let me know if you get any other info on this.
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
> I have written a Custom Message Filter. And want to trace the Form_Load or
> similar Message and write this to console. Can Anybody help me here.
[quoted text clipped - 8 lines]
> Its urgent..
> - Saurabh
Hi Saurabh,
You may implement IMessageFilter interface to get this done. For more
information and sample, please refer to the below 2 articles:
"Using IMessageFilter to create a generic filter for operating system
events"
http://www.codeproject.com/csharp/IMessageFilterArticle.asp
"Trapping windows messages"
http://www.codeproject.com/dotnet/devicevolumemonitor.asp
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Saurabh_MVP - 02 Sep 2004 10:57 GMT
Hi Jef,
I feel you didn't got my question correct. How could I trap the following
Messages using the my custom Message Filter.
WM_NULL 0x0000
WM_CREATE 0x0001
WM_DESTROY 0x0002
WM_MOVE 0x0003
WM_SIZE 0x0005
WM_ACTIVATE 0x0006
WM_ENABLE 0x000A
WM_SHOWWINDOW 0x0018
WM_MOUSEACTIVATE 0x0021
WM_ACTIVATEAPP 0x001C
My main motive is to trap a window message which comes only once when a Form
gets loaded. Few of the events are listed below.
I tried trapping the above messages, but I didn't received any of them.
Please help me, its urgent.
- Saurabh
> Hi Saurabh,
>
[quoted text clipped - 16 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
Sijin Joseph - 02 Sep 2004 14:04 GMT
Hi Jeffery,
Check out my reply, when a form is loaded only 2 messages get passed to
IMessageFilter, nothing else, as to how Form_Load is getting called, i
have detailed that in my reply.
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Jeffrey Tan[MSFT] wrote:
> Hi Saurabh,
>
[quoted text clipped - 16 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.