How to disallow tab key to switch focus between edit control and button within dialog box?(如何禁止tab键在对话框内的编辑控件和按钮之间切换焦点?)
问题描述
我有一个带有按钮和编辑框的对话框.
当编辑控件获得焦点时,如果我按 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;
}
这篇关于如何禁止tab键在对话框内的编辑控件和按钮之间切换焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何禁止tab键在对话框内的编辑控件和按钮之间切换焦点?


- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01