0
votes

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

2
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
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
I also edited the question and add part of the log file where it concerns the message.ali
Can you affirm the flow passes on the PostMessage line? Put a breakpoint on it and tell us if the execution stops there.sergiol

2 Answers

0
votes

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?

0
votes

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)