1

我试图在单行编辑控件中捕获 ENTER 和 ESC 按键。

当用户按下 ENTER 或 ESC 时,我想从编辑控件中移除键盘焦点并将其设置为 listview 控件。Listview 控件是编辑控件的兄弟。

我的目标是编写单个子类过程,可用于在主窗口和对话框中对编辑控件进行子类化。

我发现这篇 MSDN 文章因为它的第二种解决方案而很有用。以下是我对代码的改编。

// subclass procedure for edit control
LRESULT CALLBACK InPlaceEditControl_SubclassProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam,
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    switch (message)
    {
    case WM_GETDLGCODE:
        return (DLGC_WANTALLKEYS | DefSubclassProc(hwnd, message, wParam, lParam));
    case WM_CHAR:
        //Process this message to avoid message beeps.
        switch (wParam)
        {
        case VK_RETURN:
            // change focus to listview
            SetFocus(hwndListView);
            return 0L;
        case VK_ESCAPE:
            // change focus to listview
            SetFocus(hwndListView);
            return 0L;
        default:
            return ::DefSubclassProc(hwnd, message, wParam, lParam);
        }
        break;
    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_RETURN:
            // change focus to listview
            SetFocus(hwndListView);
            return 0L;
        case VK_ESCAPE:
            // change focus to listview
            SetFocus(hwndListView);
            return 0L;
        default:
            return ::DefSubclassProc(hwnd, message, wParam, lParam);
        }
        break;
    case WM_NCDESTROY:
        ::RemoveWindowSubclass(hwnd, InPlaceEditControl_SubclassProc, uIdSubclass);
        return DefSubclassProc(hwnd, message, wParam, lParam);
    }
    return ::DefSubclassProc(hwnd, message, wParam, lParam);
}

问题:

我的适应是正确的还是我错过了什么(也许SetFocus我应该WM_NEXTDLGCTLRaymond Chen指出的那样使用)?

4

0 回答 0