有一个WTL::CListViewCtrl
,显示在窗口中。当它具有焦点并按下任何键时,将调用WM_KEYDOWN/UP,但WM_CHAR。
class CPopupList : public CWindowImpl<CPopupList, WTL::CListViewCtrl>
{
public:
DECLARE_WND_SUPERCLASS(NULL, WTL::CListViewCtrl::GetWndClassName())
BEGIN_MSG_MAP(CPopupList)
MESSAGE_HANDLER(WM_GETDLGCODE, OnGetDlgCode)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
MESSAGE_HANDLER(WM_KEYUP, OnKeyUp)
MESSAGE_HANDLER(WM_CHAR, OnChar)
REFLECTED_NOTIFY_CODE_HANDLER(LVN_ITEMCHANGED, OnListItemChanged)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
LRESULT OnKeyDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
LRESULT OnKeyUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
LRESULT OnChar(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
LRESULT OnGetDlgCode(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& afx_msg LRESULT OnListItemChanged(WPARAM wParam, LPNMHDR hdr, BOOL&);
...
}
CPopupList::OnKeyDown
和CPopupList::OnKeyUp
都被调用,但CPopupList::OnChar
不是。
OnGetDlgCode
代码return DLGC_WANTALLKEYS | DLGC_WANTCHARS;
我发现,如果我将WM_KEYDOWN发布到父窗口,WM_CHAR正在父级中调用:
LRESULT CPopupList::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
::PostMessage(m_hWndParentCtrl, uMsg, wParam, lParam); -> this will call WM_KEYDOWN + WM_CHAR in parent
}
我想在列表视图中捕捉WM_CHAR。我该怎么做,我错过了什么?
ListView
不允许 键入文本。因此,它不需要产生WM_CHAR
。此特定消息通常使用翻译消息 API 从击键转换而来。您的父窗口显然会调用它。