【问题标题】:Focus not changing upon tab key in a nested CWnd-Derived Class焦点不会改变嵌套 CWnd 派生类中的制表键
【发布时间】:2015-05-12 05:47:08
【问题描述】:

环境:VS2013、MFC、C++

我有一个带有 2 个静态按钮(确定、取消)的 CDialog 派生对话框,是使用对话框编辑器创建的。此外,对话框应包含一个动态创建的 CWnd-Derived 类实例,其中包含 2 个编辑框。

问题是我无法通过 Tab 键在编辑框之间移动焦点,并且在打开对话框时我也无法使其中一个框具有初始焦点。当我按 Tab 键时,第一个编辑框会聚焦,但从此时起,我无法用 Tab 键移开焦点(用鼠标单击有效)。

我已经使用 WS_EX_CONTROLPARENT 样式创建了 CWnd,但它仍然无法移动焦点。那么问题出在哪里?到目前为止我做了什么:

//the CDialog-class which should be the container for the CWnd
//.h
class CDlgSelCatalogItem : public CDialog {
   CListFilterInput _ctrlList; //CWnd-Derived, contains 2 Edit-Boxes
}

//.cpp
BOOL CDlgSelCatalogItem::OnInitDialog()
{
   CRect rectList(10, 10, 100, 50);
   _ctrlList.Create(rectList, this);
}

//the CWnd-derived class that contains 2 edit-boxes
//.h
class CListFilterInput : public CWnd {
   BOOL Create(const RECT& rect, CWnd* pParentWnd);

   //2 Edit-Boxes   
   CEdit _ctrl1;
   CEdit _ctrl2;
}

BOOL CListFilterInput::Create(const RECT &rect, CWnd *pParentWnd)
{
   BOOL bRetVal;

   bRetVal = CWnd::CreateEx(WS_EX_CONTROLPARENT, NULL, _T(""), WS_CHILDWINDOW | WS_VISIBLE,
      rect, pParentWnd, CTRL_ID_THIS);

   if (bRetVal == TRUE){
    //1st box
    CRect rectTextbox = ...; //calculate rect fox box

    bRetVal = _ctrl1.Create(
        WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
        rectTextbox, this, CTRL_ID_TEXTBOX);

    //2nd box above 1st
    rectTextbox.MoveToY(rectTextbox.top - rectTextbox.Height());
    bRetVal = _ctrl1.Create(
        WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
        rectTextbox, this, CTRL_ID_TEXTBOX+1);

      //set input-focus initially on 1st textbox - doesnt work
      _ctrl1.SetFocus();
   }

   return bRetVal;
}

【问题讨论】:

  • 一切接缝都是正确的。从头开始编写样本对我有用。仅当 OnInitDialog 返回 FALSE 时,设置焦点才有效!否则对话处理程序会完成其余的工作。 ypu 调用 DoModal 吗?您有自己的 PreTranslateMessage 例程吗?你覆盖了 PreTranslateInput 吗?
  • 在 OnInitDialog 中返回 NULL 可以将初始焦点设置到其中一个编辑框,谢谢。但我仍然无法通过 Tab 键移动焦点;-( 是的,我确实调用 DoModal 来显示对话框,但我没有覆盖 PreTranslateMessages,无论是在 CDialog 类还是在 CWnd 派生的类中。我也没有覆盖 PreTranslateInput
  • 您尝试过我的样品并检查了差异吗?
  • 还没有。希望我明天能做到

标签: c++ mfc focus cwnd


【解决方案1】:

menu->format->tab 顺序(ctrl + d) 这个东西提供了对话框的标签顺序。一旦你设置你的订单点击对话框外。我希望它会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2010-09-27
    • 2021-11-19
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多