Solved

Windows Messaging

Posted on 1998-08-24
3
175 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
Comment Utility
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
Comment Utility
"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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

11 Experts available now in Live!

Get 1:1 Help Now