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

查看:510
本文介绍了如何禁止使用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:

  • 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天全站免登陆