Windows Messaging

Hi,
Having a little problem with VC++ 4.2 (&5.0) sending and receiving messages.  It is probably something stupid I'm doing, but I cannot send messages to my application.  I have a couple of instances.  

First off, I start a timer as this->SetTimer (1, 5000, NULL);  from the CMainFrame class initialization.  I've tried it from a few other places, such as OnOpenDoc, but this is where it is now.  This sends a message to my CMainFrame class object, and I have to put the message handlers in by hand since ClassWiz won't do it.  The CView class, on the other hand, does have a ClassWiz interface which I use to put in the OnTimer handler.  The problem is, the WM_TIMER message only goes to the MainFrame, and not the View class.  The way it's explained in the VC++ Programmers guide, the MainFrame is supposed to pass the message on to a number of other objects, but it isn't.  Is there something I need to do to get it to forward it?  

I am also having the same problems sending basic messages from anywhere in my code to my application.  Is there something undocumented to this?  I've seen example code where they just do a PostMessage with the Main windows handle, and that's all they do.  What gives?  I have tried all the versions of PostMessage, using the AfxGetMainWnd() to get the Window, passing in the handle of the window, and just using the NULL parameter to supposedly send to my app, but the messages never show up.  The return from the PostMessage says it did it, but Spy doesn't even see any message comming out, and the app doesn't respond in any of the class objects to the message.  Any help is appreciated.

Mark
mustang070597Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

milenvkCommented:
Only the command messages are routed to several objects in an MFC application. You can check the article "Command Routing" in the MSDN Library for more on this. Also messages can be routed by the windows usually to their parents or children but that should be done explicitly by these windows. You can easily solve the problem if you catch the message in the CMainFrame class and in that handler you can notify your CView derieved classes by callinf their handlers. For example:

void CMainFrame::OnTimer(UINT nIDEvent)
{
  if(nIDEvent == 1)
  {
    CDocument *pDoc = GetActiveDocument();
    if(!pDoc)
      return;
    // Notify all the views of the document that the
    // message occured
    POSITION viewPos = pDoc->GetFirstViewPosition();
    while(viewPos)
    {
      CView *pView = pDoc->GetNextView(viewPos);
      pView->SendMessage(WM_TIMER,
        (WPARAM) nIDEvent, 0L);
    }
  }
}
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
Answers2000Commented:
"First off..." etc.

1. WM_COMMAND messages get rerouted

2. WM_TIMER messages can be sent to anywindow if you set them up as such.  The MFC class will send em to the class the timer is created in or an alternative call back function you can optionally provide.  The API version of the function can send WM_TIMER messages to anywindow, provided you have a pointer to the CWnd or the hWnd of the window you want to send the messages to, example:-
::SetTimer( pWnd->m_hWnd, 1, 5000, NULL ) ;
or
::SetTimer( hWnd, 1, 5000, NULL ) ;

:: means global scope, which means use the global SetTimer function (the API version) rather than the CWnd::SetTimer function.


"I am also having...etc"
PostMessage is asynchronous (the message is queued to be processed whenever Windows feels like) a success return value means it was posted to the queue successfully, that's all

see also SendMessage which is synchronous (doesn't return until the message is processed).

Your params may be wrong, post the line if you need more help.


BTW
Milenvk's answer looks like it will work to me, but isn't strictly necessary for what I think you're doing (he's forwarding all Timer messages sent to the mainframe to all views).




0
mustang070597Author Commented:
Thanks, this looks good.  I've also had the same problems with SendMessage that I described above, but I think I've been confusing Command messages with User messages.  I've been looking at this more like a socket/PDU interface than a Windows Messaging structure and that's where I'm going wrong.  I'll accept this for now and keep working.  I'm mainly just collecting info and developing test programs before starting  a larger design/development project.  Thanks again.
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.