[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

User defined message handler..

Posted on 2000-02-02
10
Medium Priority
?
445 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:middlel
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Accepted Solution

by:
GlennDean earned 600 total points
ID: 2482871
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
 

Expert Comment

by:brandts
ID: 2482883
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
 

Author Comment

by:middlel
ID: 2483076
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:GlennDean
ID: 2483260
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2483396

 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
 
LVL 3

Expert Comment

by:GlennDean
ID: 2483496
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
 

Author Comment

by:middlel
ID: 2485071
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
 
LVL 3

Expert Comment

by:GlennDean
ID: 2486666
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2488148
RegisterWindowMessage() is only necessary if you're sending the message across processes.

..B ekiM
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2488195
I'm not so sure about that.  If an app imports 2 dialogs, both using WM_USER+10 then I see a problem.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question