Solved

Windows Messaging

Posted on 1998-08-24
3
176 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:mustang070597
3 Comments
 
LVL 2

Accepted Solution

by:
milenvk earned 150 total points
ID: 1321234
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1321235
"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
 

Author Comment

by:mustang070597
ID: 1321236
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now