Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Windows Messaging

Posted on 1998-08-24
3
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
milenvk earned 600 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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