0

I am trying to change the focus of a CDialog controls from a CFormView by using PostMessage:

[CHelpView is inherited from CFormView. And m_wndDlg is a CSampleDlg(inherited from CDialog) object]

void CHelpView::OnEnterbutton() 
{
    pSplitterFrame->m_dlgPane->m_wndDlg->PostMessage(WM_KEYDOWN, 'r', 2); 
}

BOOL CSampleDlg::PreTranslateMessage(MSG* pMsg) 
{
    if (pMsg->message >= WM_KEYFIRST && // for performance
        pMsg->message <= WM_KEYLAST)
    { 
         if (pMsg->wParam=='r' && pMsg->lParam==2){
            NextDlgCtrl();
            return TRUE; 
         }
    }
}

The dialog receives the message but the NextDlgCtrl method doesn't change the focus. I realized that if I change the PreTranslateMessage method so that if the Return key is pressed, within the dialog, this NextDlgCtrl method properly changes the focus each time the user hit the return key(from the dialog). Yet this I couldn't achieve through another dialog.

Does anyone possibly know the reason behind it or any hints or workaround ?

Thanks.

EDIT:

Here's (part of)the log file for the dialog from SPY++.

<01128> 0016013E R WM_GETDLGCODE fuDlgCode:0000

<01129> 0016013E P WM_KEYDOWN nVirtKey:00726574 cRepeat:2 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0

<01130> 0016013E S WM_NEXTDLGCTL wCtlFocus:(null) (next control receives focus) fHandle:False

<01131> 0016013E R WM_NEXTDLGCTL

<01132> 0016013E S WM_GETDLGCODE

ali
  • 529
  • 4
  • 26
  • Have you tried posting a WM_KEYUP after posting the KEYDOWN? That would more correctly mimic what happens with the mouse. Otherwise, try using Spy++ to trap the messages to the dialog. – rrirower May 20 '14 at 17:24
  • Thanks rrirower. I don't think sending the KEYDOWN message changes anything as these parameters I only use to trap this specific message in PreTranslateMessage to call NextDlgCtrl. – ali May 21 '14 at 09:46
  • I also edited the question and add part of the log file where it concerns the message. – ali May 21 '14 at 09:49
  • Can you affirm the flow passes on the `PostMessage` line? Put a breakpoint on it and tell us if the execution stops there. – sergiol May 07 '16 at 19:12

2 Answers2

0

You are posting a lParam value of 2, but checking for '2' -- they are not the same!

Try

if (pMsg->wParam == 'r' && pMsg->wParam == 2)

EDIT: realised after rrirower's answer: it should of course be

if (pMsg->wParam == 'r' && pMsg->lParam == 2)
Edward Clements
  • 5,040
  • 2
  • 21
  • 27
  • This only I wrote here mistakenly. Thanks though. – ali May 21 '14 at 09:08
  • Sorry guys for not being careful enough. These mistakes has only happened when I wrote this question here. In the project the execution actually gets to the NextDlgCtrl method. – ali May 21 '14 at 13:50
0

Your PreTranslateMessage handler, as written, will never work. This line,

if (pMsg->wParam=='r' && pMsg->wParam==2){
            NextDlgCtrl();
            return TRUE; 

can never be true. How can pMsg->wParam equate to two values at the same time? I think you meant to check for lParam?

rrirower
  • 4,338
  • 4
  • 27
  • 45