Use the Enter key to implement the effect of the TAB button in the MFC dialog box in the control input focus

xiaoxiao2021-03-06  99

Recently, write a data input interface for a data, a large number of edit boxes To implement fast input is a Tab key order that is well-designed to meet workflow. I believe, many people have such ideas and feelings when using a large number of edit boxes. And my dialog interface is primarily input is a number, so if you use the Tab key as the jump between the edit box, it is inconvenient when using the numeric keypad. Therefore, the function of the Tab key is considering using the Enter key. Because the MFC dialog features, the dialog box based on the MFC dialog box Press the Enter key to call the ONOK member function within the frame to exit. Even if you remove the BS_DEFPUSHBUTTON attribute of the IDC_OK button, it is not possible. WM_GETDEFID must be overloaded, because when the user presses the Enter key, Windows sends a WM_GETDEFID message to get the default command ID, Windows will be sent as WM_COMMAND, so the overload must return DC_HASDEFID in the high word. As described below:

Begin_MESSAGE_MAP (CMYDLG, CDIALOG) ON_MESSAGE (DM_GETDEFID, ONGETDEFID) ... END_MESSAGE_MAP ()

LResult CMYDLG :: ONGETDEFID (WPARAM WP, LPARAM LP) {Return Makelong (0, DC_HASDEFID);} This is a method, and another method is to proceed at the message loop. Rebate the PretranslateMessage this virtual function, put the message pressed in the key disk in the message queue to the block, and process the WM_KeyDown message before the framework. It is determined that after pressing the Enter key, we can use the GetNextdlgTabItem function to get the next or last control handle of the TAB key in the Tab key. The following sample code: BOOL CKeydownDlg :: PreTranslateMessage (MSG * pMsg) {if (pMsg-> message == WM_KEYDOWN && pMsg-> wParam == VK_RETURN) {CWnd * mwnd = GetNextDlgTabItem (GetFocus ()); // get the current focus Handle IF (MWND) {MWND-> setfocus () {mWnd-> setfocus (); // set the next control to get the input focus return true;}} return cdialog :: PretranslateMessage (PMSG);} But do you pay attention . After this, although the transfer of input focus can be performed at each control. However, after a button control is focused, it is also a pressing key. In the past, we used to ordered the function that should be executed after the button was pressed, and the focus came to the control after the button. This is the shortcomings of the above code, although the focus is transferred by the enterprise control, but the button cannot operate with the Enter key. Only use mice, this is not good. Moreover, in the fast data input, you cannot use the Enter key to press this button to click on the mouse to click on the first possible to make a quick purpose. Therefore, you have to make an appropriate modification for the above code. code show as below. Here, the judgment of the current focus control class is added, that is, if the current control is the button (Button), the focus jump is not performed, but constructs a WM_COMMAND message to send to the program, so that the program is that the mouse click this button. Therefore, this button has a function of the function instead of anything that does not do.

转载请注明原文地址:https://www.9cbs.com/read-103459.html

New Post(0)