PostMessage() not working

Hi,

MFC question. I put the following code into my View's OnDraw method:

static BOOL triedConnecting = FALSE;

if (!triedConnecting)
{
PostMessage(ZM_CONNECTIFREQUIRED,0,0);
triedConnecting = TRUE;
}

and manually added the following to the CMainFrame message map:

ON_MESSAGE(ZM_CONNECTIFREQUIRED,OnConnectIfCommandLineIPAddressExists)

with the appropriate method added to my code.

My new method OnConnectIfCommandLineIPAddressExists() is not being called even the PostMessage is. Am I missing something? Is there something else I should be doing?
I will need to test and confirm this but:
If you used Postmessage in the View's onDraw method - wouldn't the view be posting the message to itself and not to the CMainFrame parent??

I'm off to double check.
Last edited on
You could be right - I'm not sure myself. I thought that if you posted the message whichever class had a handler would catch it?
guestgulkan is probably right. You must have the HWND of the window you need to send the message to. If you wanted something more impersonal, there is PostThreadMessage(), but that message is posted without a window handle, meaning it must be processed at the message pump.

So yes, you must direct the message to the appropriate window.

Also: Code tags, please. http://cplusplus.com/articles/z13hAqkS/
To get the messge to the CmainFrame, I changed the postmessage to this and it worked ok:

1
2
3
4
5
6
if (!triedConnecting)
{
    ::PostMessage( GetParent()->m_hWnd, ZM_CONNECTIFREQUIRED,0,0); //using global (standard) windows message passing function
triedConnecting = TRUE;

}
Last edited on
Thanks, I tried it but couldn't get it to work for me. Still had the same problem. Spent too much time trying to get this to work so I'm trying a completely different approach. Thanks anyway.
You can always post your full code - I can't see any reason why it shouldn't work.
Topic archived. No new replies allowed.