【发布时间】:2011-08-27 03:53:01
【问题描述】:
我今天对 MFC 很满意! :D
我有一个文本框和一个列表视图控件。
当用户在文本框中按下VK_UP和VK_DOWN键时,我希望发生这种情况:
- 做一些事情。
- 让列表视图控件也处理消息(以突出显示上一个/下一个项目)。
- 我希望列表视图环绕当前选择,如果按键是其顺序中的第一个。
- 做更多的事情。
我尝试在我的对话框中对我的编辑框进行子类化:
class MyEditBox : public CWnd
{
bool allowWrap;
afx_msg void OnKeyUp(UINT, UINT, UINT) { this->allowWrap = true; }
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CListCtrl &listView = static_cast<CListView *>(
this->GetParent()->GetDlgItem(IDC_LIST_VIEW))->GetListCtrl();
if (nChar == VK_UP || nChar == VK_DOWN)
{
int iSelBefore = listView.GetNextItem(-1, LVNI_SELECTED);
this->GetParent()->GetDlgItem(IDC_LIST_VIEW)
->OnKeyDown(nChar, nRepCnt, nFlags); //Oops! Protected member :(
int iSelAfter = listView.GetNextItem(-1, LVNI_SELECTED);
if (iSelBefore == iSelAfter && // Did the selection reach an end?
this->allowWrap) // If so, can we wrap it around?
{
int i = a == 0 ? listView.GetItemCount() - 1 : 0;
listView.SetItemState(i, LVIS_SELECTED | LVIS_FOCUSED,
LVIS_SELECTED | LVIS_FOCUSED);
}
}
this->allowWrap = false;
}
}
但是OnKeyDown() 是一个受保护的成员,所以我不能只在另一个控件上调用它。
有没有比使用SendMessage 手动发送命令更好 的方法来解决这个问题?我应该改变我的设计吗?子类化其他东西,等等?
【问题讨论】:
标签: winapi visual-c++ mfc