Grabing the FormLoad Message from the Message Queue

S

Saurabhdotnet

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
 
S

Sijin Joseph

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/d.../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
 
J

Jeffrey Tan[MSFT]

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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

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
 
J

Jeffrey Tan[MSFT]

Hi Saurabh,

Thanks for your feedback.

Oh, yes, I really reproduced out your problem. I will spend a little more
time on this issue. I will reply to you ASAP.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Saurabh,

Sorry for letting you wait for so long time.

Actually, IMessageFilter is pre process before the normal translate /
dispatch cycle of the message pump, it will only see posted messages. While
the messages you listed are all pass to the Windows procedure through
SendMessage method, which will not be placed in the message pump. So the
IMessageFilter can not intercept them.

You may try to intercept these messages through overriding Form's WndProc
method, like this:
protected override void WndProc(ref Message m)
{
Console.WriteLine(m.Msg.ToString());
base.WndProc (ref m);
}

=================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Saurabh,

Does my reply make sense to you? Do you still have concern on this issue?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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