Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

Grabing the FormLoad Message from the Message Queue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saurabhdotnet - 01 Sep 2004 12:36 GMT
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
Sijin Joseph - 01 Sep 2004 16:54 GMT
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
"Jeffrey Tan[MSFT]" - 02 Sep 2004 08:49 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.