User defined message handler..

Hi,

I have an MFC dialog application and I wish to do something as soon as OnInitDialog() has finished. As there is no PostInitDialog() event, I opted for a hidden button and then Posting a BN_CLICKED message for the hidden button. There must be an easy way to create your own message handlers...???

can anyone tell me how that can be done. I think the hidden button is a hack. :-)

thanks

Emma
middlelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GlennDeanCommented:
Emma:
   You can create user messages and have a handler for them.
   The way I've done it is, in my stdafx.h file add the line
   #define WM_MYMESSAGE (WM_USER+10)
and then you can send that message via SendMessage or PostMessage as in
m_pWindow->PostMessage(WM_MYMESSAGE,"id of button",0);
  Here m_pWindow is of type CWnd * and is the CWnd * of the window that has the button.  
   Then, create a handler for the message, making sure it's prototype is
afx_msg LRESULT MyMessageHandler(WPARAM wParam, LPARAM lParam);
   Finally, inside the BEGIN_MESSAGE_MAP (but outside the AFX_MSG_MAP brackets - this is important) add
ON_MESSAGE(WM_MYMESSAGE,MyMessageHandler)

    Glenn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brandtsCommented:
In this case it is important that you *post* the message, as opposed to *sending* it.  The difference is that PostMessage adds the message to the application's message queue and returns immediately.  SendMessage adds the message to the queue, but waits until the message has been processed to return.
0
middlelAuthor Commented:
It compiles, but I never receive the actual event..??

my header looks like:
// Message Map Handlers
afx_msg LRESULT      OnInitialLoad( WPARAM wParam, LPARAM lParam );

cpp looks like:
//}}AFX_MSG_MAP
ON_MESSAGE(WM_INITIAL_LOAD, OnInitialLoad)
END_MESSAGE_MAP()

WM_INITIAL_LOAD = 11000001.
and I Posted the event like this:
PostMessage( WM_INITIAL_LOAD );

Hope you can help... I don't know why it won't handle the event..?

thanks

Emma



0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

GlennDeanCommented:
You have to Post the Message to a particular window.
   Here's some additional info that's more to what you want to do.
   Handle the WM_SHOWWINDOW message (you're OnInitDialog will complete before this message handler executes).  There, add these line of code
  CWnd * pWnd = FromHandle(m_hWnd);
  pWnd->PostMessage(WM_INITIAL_LOAD);

    Glenn
0
mikeblasCommented:

 GlennDean> There, add these line of code
 GlennDean>  CWnd * pWnd = FromHandle(m_hWnd);
 GlennDean>  pWnd->PostMessage(WM_INITIAL_LOAD);

Huh?  Why would you do that?  If you're in the OnInitDialog() handler, and you want to send the message to the dialog itself, just use the implicit this pointer!

   PostMessage(WM_INITIAL_LOAD);

is equivalent to

   this->PostMessage(WM_INITIAL_LOAD);

which does the same thing you're suggsting--just in a far more direct way! There's absolutely no need for a call to FromHandle().

 emma> WM_INITIAL_LOAD = 11000001.

Your message ID must be less than 0xFFFF (which is 65535 decimal).

..B ekiM
0
GlennDeanCommented:
Just noticed your value of WM_INITIAL_LOAD isn't in the proper range (from 0x0400 to 0x7fff).  0x0400 is the value of WM_USER.  Your value of 0b11000001 is less than 0x0400.  That might be the problem.

Mike:  You are right.  I'm so use to using ::PostMessage as opposed to PostMessage so calling PostMessage as Emma has done from the WM_SHOWWINDOW handler will work.
0
middlelAuthor Commented:
thanks a lot..

I thought I could set the value of the message to anything :-).

I searched the help for the page on WM_USER and it explained all the different ranges.. It's difficult to search the help when you really don't know what you are looking for..

Thanks again..

Emma
0
GlennDeanCommented:
Emma:
   There is another way to make a new message number besides using something like
   WM_USER+10
The technique is to call RegisterWindowMessage which will give you a unique message id number for the entire system.
   Glenn
0
mikeblasCommented:
RegisterWindowMessage() is only necessary if you're sending the message across processes.

..B ekiM
0
GlennDeanCommented:
I'm not so sure about that.  If an app imports 2 dialogs, both using WM_USER+10 then I see a problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.