1

当用户在组合框中按 Enter 时,我想要捕获事件。怎么抓你。例如:我在组合框中输入“Nguyen Phong Sac”。按 Enter 键后,会显示一条消息:“Nguyen Phong Sac”。谢谢你的帮助,

4

3 回答 3

8

我仍然想知道你在写什么样的程序,但我会按照你的要求给你一个例子:

//Tested with Windows 7 x64, VS2012
//When Creating:
hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""), 
               CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
               xpos, ypos, nwidth, nheight, hwndParent, NULL, HINST_THISCOMPONENT,
               NULL);

hWndEditBox = CreateWindow(WC_EDIT, TEXT(""), 
              CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
              xpos, ypos-30, nwidth, 30, hwndParent, NULL, HINST_THISCOMPONENT,
              NULL);

//Get hwnd of edit control in combobox created earlier.
HWND hwndEdit = GetWindow(hWndComboBox, GW_CHILD);
//Use SetWindowLong to create subclass, lpfnEditWndProc is original proc
lpfnEditWndProc = (WNDPROC) SetWindowLong(hwndEdit, GWL_WNDPROC, (DWORD) SubClassProc); 


//In Subclass Proc
LRESULT CALLBACK SubClassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
   switch (msg) 
   { 
    case WM_KEYDOWN: 
        switch (wParam) 
        {  
            case VK_RETURN: 
                                    //Get Text&Set Text
                LPTSTR buffer = new TCHAR[255];
                GetWindowText(hwnd, buffer, 255);
                SetWindowText(hWndEditBox, buffer);
            break;
        } 
        break; 

  } 

  return CallWindowProc(lpfnEditWndProc, hwnd, msg, wParam, lParam); 
} 
于 2013-03-27T09:10:23.903 回答
0

你也可以在 WTL 中实现这一点

#define YOUR_WM_ENTER WM_USER + 2

class CCommandBarEdit : public CWindowImpl<CCommandBarEdit, CEdit, CControlWinTraits>
{
    typedef CWindowImpl<CCommandBarEdit, CEdit, CControlWinTraits> baseClass;

public:
    HWND hwndMain;

    DECLARE_WND_SUPERCLASS(_T("YOUR_COMMANDBAREDIT"), _T("EDIT"));

    BEGIN_MSG_MAP(CCommandBarEdit)
        MESSAGE_HANDLER(WM_CHAR, OnKeyDown)
        DEFAULT_REFLECTION_HANDLER()
    END_MSG_MAP()

private:
    LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        if (wParam == VK_RETURN && GetFocus() == m_hWnd)
        {
            ::SendMessage(hwndMain,  YOUR_WM_ENTER,0,0);
        }
        else
        {
            bHandled = FALSE;
        }

        return 0;
    }
};

在您保留编辑容器的位置,m_Combo 是您的组合框

CCommandBarEdit m_cmdTextBox;

HWND hWndEdit = ::GetWindow(m_Combo.m_hWnd, GW_CHILD);
    if (hWndEdit)
        m_cmdTextBox.SubclassWindow(hWndEdit);

您在哪里回复消息

MESSAGE_HANDLER(YOUR_WM_ENTER, OnCommandEnter)
LRESULT CMainFrame::OnCommandEnter(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
于 2017-03-15T14:57:17.237 回答
0

我知道那是 2 晚了,但你可以使用 WndProc

        protected override void WndProc(ref Message m)
        {
              if (m.Msg == 0x100 && (int)m.WParam == 13){
                    //Enter pressed
              }

              base.WndProc (ref m);
        }
于 2016-08-03T11:53:41.847 回答