?
Solved

PreTranslateMessage or Message Maps in CWinAp derived classes

Posted on 2003-03-31
6
Medium Priority
?
1,281 Views
Last Modified: 2013-11-20
I'm currently trying to sort out some historic code which has something like this:

BOOL CMyWinApp2::PreTranslateMessage(MSG* pMsg)
{
  if (pMsg->message == WM_DRIVER_FIRSTNOTE)
  {
    OnFirstNote(pMsg->wParam, pMsg->lParam);
  }
  return CMyWinApp1::PreTranslateMessage(pMsg);
}

WM_DRIVER_FIRSTNOTE is a custom registered windows message.
CMyWinApp2 is derived from a class (CMyWinApp1) that is derived from CWinApp. All the inheritance seems fine.

There is also a message map for WM_DRIVER_FIRSTNOTE:

BEGIN_MESSAGE_MAP(CMyWinApp2, CMyWinApp1)
  //{{AFX_MSG_MAP(CMyWinApp2)
     ON_REGISTERED_MESSAGE(WM_DRIVER_FIRSTNOTE, OnFirstNote)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

The prototype for OnFirstNote is:

afx_msg LRESULT OnFirstNote(WPARAM, LPARAM);

The message in question originates from a list control on a property page on a modal dialog. The property sheet and the dialog both have the 'No parent notify' option disabled in the resource, if that's of significance. I assume that option would if enabled stop the message being passed back to the app?

An now for my question:

As I understand it PreTranslateMessage is not the correct way to map messages like this. (I'm not bothered about disputing this, there are several topics out there to support my claim, unless someone truely knows what they're talking about). Given this when I remove the PreTranslateMessage it doesn't call OnFirstNote?!? The message isn't handled anywhere else apart from in the main ap code, and as I understand it, it should get passed to my CMyWinApp2 class and map to OnFirstNote.

What am I missing/not understanding correctly?




0
Comment
Question by:jfstephe
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8238988
BOOL CMyWinApp2::PreTranslateMessage(MSG* pMsg)
{
 if (pMsg->message == WM_DRIVER_FIRSTNOTE)
 {
   OnFirstNote(pMsg->wParam, pMsg->lParam);
   return TRUE;
 }
 return CMyWinApp1::PreTranslateMessage(pMsg);
}

Adding line
return TRUE;
stops handling the message. Without this line message may be handled in one of program classes.

PreTranslateMessage is usually used for the messages which cannot be handled by other way or to prevent standard MFC handling of some messages.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 8239712

BEGIN_MESSAGE_MAP(CMyWinApp2, CMyWinApp1)
 //{{AFX_MSG_MAP(CMyWinApp2)
    ON_REGISTERED_MESSAGE(WM_DRIVER_FIRSTNOTE, OnFirstNote)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

Take the Registerd Message out of the AFX_MSG_MAP Block

BEGIN_MESSAGE_MAP(CMyWinApp2, CMyWinApp1)
 //{{AFX_MSG_MAP(CMyWinApp2)
 //}}AFX_MSG_MAP
    ON_REGISTERED_MESSAGE(WM_DRIVER_FIRSTNOTE, OnFirstNote)
END_MESSAGE_MAP()






0
 
LVL 9

Assisted Solution

by:Priyesh
Priyesh earned 375 total points
ID: 8242511
try mapping the ON_REGISTERED_MESSAGE in your CMainFrame or the mainwindow class of your app whatsoever.

0
 

Author Comment

by:jfstephe
ID: 8254853
williamcampbell - I will do when I get a chance. I doubt that it will resolve it but it's a good point.

Priyesh - Why would this change anything? If the PreTranslateMessage in CMyWinApp2 can see the message then why isn't it getting map across?

Is my general understanding correct or am I missing something?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 375 total points
ID: 8258809
ON_REGISTERED_MESSAGE should be used in some CWnd-derived class and not in CWinApp-derived class. Move this handler to the window to which this message is posted (CMainFrame?)
On the other way, CWinApp::PreTranslateMessage is the place where you can catch any message posted to any window in the application.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

621 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