Link to home
Start Free TrialLog in
Avatar of SkizoWalker
SkizoWalker

asked on

Getting message from child control

Hi .. I create a class CInformation derided from CWnd
from this CInformation i create a variable which i call 'm_pEdit' and which is type of CEdit*
when the focus is set to my m_pEdit variable , how could the parent window (CInformation) knows the user type the key : -Return or Escape- from the child control ?

Please help .. i try so much stoof , AS WM_NOTIFY or other stoof like this and still didn't success ...
ASKER CERTIFIED SOLUTION
Avatar of tma050898
tma050898
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For the parent (CInformation) to know that Return or Escape was hit when the m_pEdit had focus.

BOOL CInformation::PreTranslateMessage(MSG* pMsg)
{  
 BOOL bHandledMsg = FALSE;

 switch (pMsg->message)
 {                      
  case WM_KEYDOWN:
      {
   // Get the CWnd* for the current child control
   CWnd* pwndCurrent = NULL;
   pwndCurrent = GetFocus();              
   // here you can compare pwndCurrent.GetSafeHwnd() to m_pEdit.GetSafeHwnd()
       // if you care that it was the m_pEdit control that had focus

   switch (pMsg->wParam)
   {
    case VK_ESCAPE:
     // your Esc code here
     bHandledMsg = TRUE;
    break;

    case VK_RETURN:  
     // your Return, or Enter code here
     bHandledMsg = TRUE;                  
    break;

    default: break;
  } // switch (pMsg->wParam)
  break;

  default: break;                  
 } // switch (pMsg->message)                  

 // continue normal translation and dispatching            
 return (bHandledMsg ? TRUE : (CInformation Base Class)::PreTranslateMessage(pMsg);
}

SkizoWalker,

Couple of corrections and comments.

I should mention that you should use ClassWizard to create a PreTranslateMessage override and then modify the PreTranslateMessage as follows. I left comments where you probably want to put your own application specific code. Lemme know if you need any additional information.

Understand that by returning TRUE from the PreTranslateMessage, you are saying that you DO NOT want processing to continue. Therefore, for example, if you placed this code into an AppWiz generated dialog-based app right now, pressing the Enter key or the Esc key would not do anything as far as dismissing the dialog. This is because the PreTranslateMessage function is "eating" those keystrokes by return TRUE. Obviously, what you do with these keys is app specific.

BOOL CInformation::PreTranslateMessage(MSG* pMsg)
{  
 BOOL bHandledMsg = FALSE;

 switch (pMsg->message)
 {                      
  case WM_KEYDOWN:
  {
   // Get the CWnd* for the current child control
   CWnd* pwndCurrent = NULL;
   pwndCurrent = GetFocus();              
   // here you can compare pwndCurrent.GetSafeHwnd() to m_pEdit.GetSafeHwnd()
   // if you care that it was the m_pEdit control that had focus

   switch (pMsg->wParam)
   {
    case VK_ESCAPE:
     // your Esc code here
     bHandledMsg = TRUE;
    break;

    case VK_RETURN:  
     // your Return, or Enter code here
     bHandledMsg = TRUE;                  
    break;

    default: break;
   } // switch (pMsg->wParam)
  } // WM_KEYDOWN
  break;

  default: break;                  
 } // switch (pMsg->message)                  

 // continue normal translation and dispatching            
 return (bHandledMsg ? TRUE : (CInformation Bas Class::PreTranslateMessage(pMsg));
}
Avatar of SkizoWalker
SkizoWalker

ASKER

thanx very much for such an answer .. here the points you had the real merit to win .. ur welcome ;) anyway i didn't use aPPwizard to create this class (CInformation : public Cwnd) , so what is the correct function defintion i should use ?
afx_msg BOOL PreTranslateMessage(MSG* pMsg) ? hope it gonna works ...

cya ,
Mickael
France

http://wwwperso.hol.fr/~mgilaber/ for some OpenGL / Glide sourcecode and sample of my own!

No. It's a virtual function...

virtual BOOL PreTranslateMessage(MSG* pMsg);

i have troubel .. when i try to implement this method in my CWnd derived class  , it nevers calls the function PreTranslateMsg ... how could i make sure this function will be called at each message ?? !
1. Did you make the function virtual?

virtual BOOL PreTranslateMessage(MSG* pMsg);

2. How are you creating this window? IOW, can you post the code as to how you are creating and displaying this window?
okay here is the code i did :

CWndSaisie::CWndSaisie(CWnd* parent)
{
m_rect.top = 175;
m_rect.left = 160;
m_rect.right = m_rect.left + 450;
m_rect.bottom = m_rect.top + 250;

CString wndClass;
wndClass = AfxRegisterWndClass(0,NULL,(HBRUSH) (COLOR_WINDOW),NULL);


DWORD dwStyle   = WS_OVERLAPPED | WS_BORDER | WS_DLGFRAME | WS_VISIBLE | WS_CAPTION | WS_SYSMENU;
DWORD dwStyleEx = WS_EX_TOOLWINDOW;

CreateEx(dwStyleEx,wndClass,"Saisie",dwStyle,m_rect,parent,NULL);
ShowWindow(TRUE);
}

hope it could help you .