Win32 Dialog dialog box handles WM_KEYDOWN events

Source: Internet
Author: User

In MFC, overriding virtual functions in PreTranslateMessage can filter wm_keydown messages. However, if you are using Dialogbox/dialogboxparam to create modal dialogs in the Win32 SDK, the general behavior is not to process WM_KEYDOWN messages.If there are no child controls in the dialog box, the window callback function Dlgproc () in the dialog box can be filtered to the WM_KEYDOWN message., but not all of the key messages can be captured, such as the TAB key, the upper and lower left and right arrow keys vk_up/vk_down/vk_left/vk_right and so on have no way to capture.

If there is a child control in the modal dialog box, even if it is a static text control, there is no way to get any wm_keydown message in the window callback function of the dialog box.
So the modal dialog box can use the following method if it wants to handle the TAB key/vk_up/down/left/rigth without any child controls:

INT_PTR CALLBACK Dlgproc (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {Case wm_initdialog:{oldwndproc = (WNDPROC) SetWindowLong (Hdlg, GWL_WNDPROC, (LONG) newdlgproc);// Sets the new window procedure callback function}return (INT_PTR) TRUE;}     // ...  Other message processing, here omit return (INT_PTR) FALSE;} LRESULT CALLBACK Newdlgproc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)//The dialog box has a new window callback function that filters the WM_KEYDOWN messages. {switch (message) {case wm_getdlgcode:{  return (Dlgc_wantallkeys | CallWindowProc (Oldwndproc, hWnd, message, WParam, LParam)); Note here, otherwise there is no way to capture the tab/direction key}case Wm_keydown:{tchar Sztext[max_path] = {0}; stringcchprintf (Sztext, _countof (Sztext), _t ("%d"), WParam); SetWindowText (HWnd, sztext);} Break;default:break;} Return CallWindowProc (Oldwndproc, hWnd, message, WParam, LParam);}


Add: If there are other child controls on the dialog box, use the method and its type, subclass, set the control's new window callback function, and the new window callback function is similar to the one above. For example, there is a static text control in the dialog box, which is how to capture the tab/arrow key event.


Win32 Dialog dialog box handles WM_KEYDOWN events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.