I have dialog box having buttons and edit box.
When edit control have focus then if I press tab key it moves and focus the button.
I wanted tab key work in such a way that it will not switch focus instead it should work as tab input inside edit control i.e. input to edit box as keys.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The solution is fairly simple, and essentially consists of handling the WM_GETDLGCODE message. This allows a control implementation to fine-tune keyboard handling (among other things).
In MFC this means:
- Derive a custom control class from CEdit.
- Add the ON_WM_GETDLGCODE message handler macro to the message map.
- Implement the OnGetDlgCode member function, that adds the
DLGC_WANTTAB
flag to the return value. - Subclass the dialog's control, e.g. using the DDX_Control function.
Header file:
class MyEdit : public CEdit {
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg UINT OnGetDlgCode();
};
Implementation file:
BEGIN_MESSAGE_MAP(MyEdit, CEdit)
ON_WM_GETDLGCODE()
END_MESSAGE_MAP
UINT MyEdit::OnGetDlgCode() {
UINT value{ CEdit::OnGetDlgCore() };
value |= DLGC_WANTTAB;
return value;
}
回答2:
Override the PreTranslateMessage function in your dialog like this :
BOOL CTestThreadDlg::PreTranslateMessage( MSG* pMsg )
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB)
{
CWnd* pFocusWnd = GetFocus( );
if (pFocusWnd != NULL && pFocusWnd->GetDlgCtrlID() == IDC_EDIT2)
{
CEdit *pEditCtrl = (CEdit *)pFocusWnd ;
int start, end ;
pEditCtrl->GetSel(start, end) ;
CString str ;
pEditCtrl->GetWindowText(str) ;
str = str.Left(start) + _T("\t") + str.Mid(end) ;
pEditCtrl->SetWindowText(str) ;
pEditCtrl->SetSel(start + 1, start + 1) ;
}
return TRUE ;
}
return CDialog::PreTranslateMessage(pMsg) ;
}
In this example we check if the focus is in the IDC_EDIT2 edit control. You probably have to adapt this to your situation.