为什么我的ListView中没有调用WM_CHAR

时间:2017-03-23 13:24:17

标签: listview mfc event-handling wtl clistctrl

有一个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::OnKeyDownCPopupList::OnKeyUp都被调用,但CPopupList::OnChar不是。

OnGetDlgCode代码为return DLGC_WANTALLKEYS | DLGC_WANTCHARS;

我发现,如果我将WM_KEYDOWN发布到Parent窗口,则在Parent中调用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
}

我想在ListView中捕获WM_CHAR。我怎么能这样做,我错过了什么?

1 个答案:

答案 0 :(得分:1)

ListView不允许输入文字。因此,它不需要生成 WM_CHAR。此特定消息通常使用TranslateMessage API从击键转换而来。您的父窗口显然会调用它。