Plus Accelerator (shortcut) for dialog box program

xiaoxiao2021-03-06  116

method one:

http://support.microsoft.com/?kbid=222829

1 Insert a new Accelerator to the resource, associate the acceleration key and the corresponding response control (such as a button)

2 Declaring in the dialog box header file:

Haccel M_Haccel;

3 Initialization M_Haccel in the constructor of the dialog

M_haccel = :: loadaccelerators (AFXGETITISTANCEHANDE (), makeintResource (iDR_Accelerator1);

4 and then overload the PretranslateMessage function of the dialog,

Bool Caboutdlg :: PretranslateMessage (MSG * PMSG)

{

IF (m_hacceltable)

{

IF (: TranslateAccelerator (M_HWND, M_HACCELTABLE, PMSG))

{

Return (TRUE);

}

}

Return CDIALOG :: PretranslateMessage (PMSG);

}

Method Two:

1 Declaration hot key message processing function prototype

The message mapping declaration at .h is added (after AFX_MSG font) to join the following statement:

LResult Onhotkey (WParam WPARAM, LPARAM LPARAM);

2. The message is associated with the corresponding processing function

Add message mapping macros in .CPP to make the message relationship with the corresponding processing function.

ON_MESSAGE (WM_HOTKEY, INHOTKEY);

3. Operation for convenience

Create a function of responding to WM_CREATE and WM_DESTROY messages in advance

ONCREATE () frame with onDestroy ()

4. Register the hotkey to the system

Add the following code to the system to register the hotkey to the system, the hotkey of this example is set to

Ctrl Shift a.

RegisterhotKey (M_HWND, 1001, MOD_CONTROL | MOD_SHIFT, 'A');

RegisterhotKey (M_HWND, 1002, MOD_CONTROL | MOD_SHIFT, 'A');

5. Treatment hotkey

Process the hotkey in the message processing function on /otkey (), and can join the program code that the user wants to run

Lresult c ***** :: onhotkey (WPARAM WPARAM, LPARAM LPARAM)

IF (wparam == 1001 || wparam == 1002)

CWnd :: setForegroundWindow (); // Make the activated window appears in the foreground

MessageBox ("Hello!");

// Users can add code here

Return 0;

6. After the program is running, the hotkey is released.

Release the system resources in OnDestroy () to release the system resources through UnregisterhotKey ().

UnregisterhotKey (M_HWND, 1001);

UnregisterhotKey (M_HWND, 1002);

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

New Post(0)