Tweak subclass procedure so it can be used both in window and dialog box

给你一囗甜甜゛ 提交于 2019-12-24 13:24:49

问题


I am trying to catch ENTER and ESC key press in singleline edit control.

When user presses ENTER or ESC I want to take away keyboard focus from edit control and set it to listview control. Listview control is edit control's sibling.

My goal is to write single subclass procedure that can be used for subclassing edit controls in both main window and dialog box.

I have found this MSDN article that I found useful because of its second solution. Below is my adaptation of the code.

// 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);
}

QUESTION:

Is my adaptation correct or am I missing something (maybe instead of SetFocus I should use WM_NEXTDLGCTL like Raymond Chen pointed out)?

来源:https://stackoverflow.com/questions/30294294/tweak-subclass-procedure-so-it-can-be-used-both-in-window-and-dialog-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!