问题
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