Home > Software engineering >  MFC button setFocus () cannot be set focus, edit box
MFC button setFocus () cannot be set focus, edit box

Time:09-22

A little programming basis, this paragraph of time has been learning MFC dialog, my original idea is in the open dialog box press ENTER circulation set each TABSTOP property control focus, dialog now a total of 8 is the space, I press the TAB key to switch focus properly, and then press the ENTER key will respond to the current space message processing function with a focus, this is right I also understand, but by running the code I wrote the ENTER key can only be set focus on the edit box, a button control I debug, actually also has run setfocus () function, just didn't respond, look effect should have eight controls take turns to be the focus but now there is four times the ENTER key on the button no effect, but the function also ran away, I also saw setfocus values, including yao m_hWnd and the return value is also effective, so it was very confused, please understand people give answer, thank you,

And I noticed that the role of the press the TAB key can respond to the current button, for the same reason I press the enter key can only seem to setfocus, rather than the button "activate",

The following is a snippet, is very simple,
 void CCalcDlg: : OnBnClickedOk () 
{
//TODO: add the control notification handler code
//CDialogEx: : OnOK ();
//# define OKflag NULL
//int intRet=OKflag;
//EndDialog (intRet);
//callback functon of the default control box
The CWnd * p1=GetFocus ();
The CWnd * p2=GetNextDlgTabItem (p1);
The CWnd * p3=p2 - & gt; SetFocus ();
Cstrings PTR=__T (" ");
The p2 - & gt; GetWindowTextW (PTR);
//MessageBox (PTR. Get string ());
//GetFocus () - & gt; GetNextWindow () - & gt; SetFocus ();
}


//afx_msg LRESULT CCalcDlg: : OnInitdialog (WPARAM WPARAM, LPARAM LPARAM)
//{
//return 0;
//}

WNDPROC oldWindowproc;
LRESULT a CALLBACK newWindowproc (
The HWND HWND,
UINT uMsg,
WPARAM WPARAM,
LPARAM LPARAM)
{
BOOL ret=false;
If (uMsg==WM_CHAR & amp; & WParam==0 x0d)
{
(: : SetFocus GetNextWindow (HWND, GW_HWNDNEXT));
Ret=true;
return ret;
}
The else
{
Return CallWindowProc (
OldWindowproc,
The HWND,
UMsg,
WParam,
LParam);
}
//oldWindowproc (
//the HWND,
//uMsg,
//wParam,
//lParam);

}



Afx_msg LRESULT CCalcDlg: : OnInitdialog (WPARAM WPARAM, LPARAM LPARAM)
{
CDialogEx: : OnInitDialog ();
Pcombox1=(CComboBox *) GetDlgItem (IDC_COMBO1);
if (! Pcombox1 - & gt; GetCount ())
{
Pcombox1 - & gt; InsertString (0, __T (" add "));
Pcombox1 - & gt; InsertString (1, __T (" cut "));
Pcombox1 - & gt; InsertString (2, __T (" on "));
Pcombox1 - & gt; InsertString (3, __T (" addition "));
}
Pcombox1 - & gt; SetCurSel (0);
GetDlgItem (IDC_EDIT2) - & gt; SetFocus ();
//: : SetFocus (GetDlgItem (IDC_EDIT2) - & gt; M_hWnd);
//oldWindowproc=(WNDPROC) SetWindowLongPtr (GetDlgItem (IDC_EDIT2) - & gt; M_hWnd, GWLP_WNDPROC, (long) newWindowproc);
//it is very inportant here to set the return value FALSE, otherwise we the default focus will always be set
Return FALSE;
}


CodePudding user response:

 
BOOL CFocus_testDlg: : PreTranslateMessage (MSG * pMsg)
{
//TODO: Add your specialized code here and/or call the base class

If (WM_KEYDOWN==pMsg - & gt; Message)
{
The CWnd * pFocus=GetFocus ();
If (pFocus)
{
If (IDC_BUTTON1==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON2) - & gt; SetFocus ();
}
Else if (IDC_BUTTON2==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON3) - & gt; SetFocus ();
}
Else if (IDC_BUTTON3==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON1) - & gt; SetFocus ();
}
return TRUE;
}
}

Return CDialog: : PreTranslateMessage (pMsg);
}

CodePudding user response:

CodePudding user response:

Press enter button to get the focus should be equivalent button clicks, without OnBnClickedOk

CodePudding user response:

reference 1st floor jennyvenus response:
 
BOOL CFocus_testDlg: : PreTranslateMessage (MSG * pMsg)
{
//TODO: Add your specialized code here and/or call the base class

If (WM_KEYDOWN==pMsg - & gt; Message)
{
The CWnd * pFocus=GetFocus ();
If (pFocus)
{
If (IDC_BUTTON1==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON2) - & gt; SetFocus ();
}
Else if (IDC_BUTTON2==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON3) - & gt; SetFocus ();
}
Else if (IDC_BUTTON3==pFocus - & gt; GetDlgCtrlID ())
{
(CButton *) (GetDlgItem (IDC_BUTTON1) - & gt; SetFocus ();
}
return TRUE;
}
}

Return CDialog: : PreTranslateMessage (pMsg);
}

I tried this preTranslateMessage () also not line, or the button on the setfocus () doesn't work, but when I set the breakpoint on setfocus () function can see behind the button was focused, I am what all can't see the normal running process, the button have no reaction, I the original code, don't know why, hope answer confused, thank you

CodePudding user response:

I this is reference Sun Xin video tutorial I did example, environment is VS2015, don't update something?

CodePudding user response:

Ask, how do you do the TAB to switch, I use the CTRL + D switching sequence set, use preTranslateMessage intercept the message, but always click on the keyboard and not into preTranslateMessage, don't know why, I this is child dialogs, in the upper dialog also added a preTranslateMessage for message filtering, but never into preTranslateMessage this function

CodePudding user response:

OnBnClickedOk ()
nullnullnullnullnullnullnullnullnullnullnullnullnullnull