如何禁止tab键在对话框内的编辑控件和按钮之间切换焦点? [英] How to disallow tab key to switch focus between edit control and button within dialog box?

查看:10
本文介绍了如何禁止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键在对话框内的编辑控件和按钮之间切换焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆