How to respond to keyboard buttons in the dialog

zhaozj2021-02-08  270

What we first think of responding to WM_KeyDown messages, but actually runs without any effect.

The reason is that the controls in the dialog need to respond to the button, such as the multi-line edit box must first

Handling Entering, not allowing the bus to close the dialog.

We want to respond to the button on the dialog in the first time, you need to overload PretranslateMessage.

The following code implements the virtual key code in the dialog box (Virtual-Key Code)

Bool ctestdlg :: PretranslateMessage (MSG * PMSG)

{

IF (PMSG-> Message == WM_KeyDown)

{

/ / / Or directly call onkeydown

Cstring strwaM;

Strwparam.Format ("% D", PMSG-> WPARAM);

CDC * PDC = Getdc ();

PDC-> Textout (10, 10, strwparam);

ReleaseDC (PDC);

}

Return CDIALOG :: PretranslateMessage (PMSG);

}

This method also applies to the response to the keyboard button in FormView or other controls, the following code comes from MSDN,

Implemented onkeyDown when pressing the left and right direction keys, we can work on OnkeyDown.

Bool CsampleControl :: PretranslateMessage (lpmsg lpmsg)

{

Bool bhandlenow = false;

Switch (lpmsg-> message)

{

Case WM_KeyDown:

Switch (lpmsg-> wparam)

{

Case VK_UP:

Case vk_down:

Case VK_LEFT:

Case vk_right:

Bhandlenow = True;

Break;

}

IF (bhandlenow)

OnkeyDown (LPMSG-> WPARAM, LOWORD (lpmsg -> lparam), HiWord (lpmsg-> lparam);

Break;

}

Return Bhandlenow;

}

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

New Post(0)