Link to home
Start Free TrialLog in
Avatar of middlel
middlel

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of GlennDean
GlennDean

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brandts
brandts

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.
Avatar of middlel

ASKER

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



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

 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
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.
Avatar of middlel

ASKER

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
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
RegisterWindowMessage() is only necessary if you're sending the message across processes.

..B ekiM
I'm not so sure about that.  If an app imports 2 dialogs, both using WM_USER+10 then I see a problem.