【发布时间】: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
-
您尝试过我的样品并检查了差异吗?
-
还没有。希望我明天能做到