如何禁止tab键在对话框内的编辑控件和按钮之间切换焦点?
2022-01-12 00:00:00
mfc
我有一个带有按钮和编辑框的对话框.
当编辑控件获得焦点时,如果我按 Tab 键,它会移动并聚焦按钮.
我希望 tab 键的工作方式不会切换焦点,而是应该作为编辑控件内的 tab 输入,即作为键输入到编辑框.
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.
推荐答案
解决方案相当简单,主要包括处理 WM_GETDLGCODE 消息.这允许控件实现微调键盘处理(除其他外).
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).
在 MFC 中,这意味着:
In MFC this means:
- 从 CEdit 派生自定义控件类.
- 添加 ON_WM_GETDLGCODE消息映射的消息处理程序宏.
- 实现 OnGetDlgCode 成员函数,将
DLGC_WANTTAB
标志添加到返回值. - 子类化对话框的控件,例如使用 DDX_Control 功能.
- 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.
头文件:
class MyEdit : public CEdit {
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg UINT OnGetDlgCode();
};
实现文件:
BEGIN_MESSAGE_MAP(MyEdit, CEdit)
ON_WM_GETDLGCODE()
END_MESSAGE_MAP
UINT MyEdit::OnGetDlgCode() {
UINT value{ CEdit::OnGetDlgCore() };
value |= DLGC_WANTTAB;
return value;
}
相关文章