C #, MFC, Win32 -- implement system-level hotkeys

Source: Internet
Author: User

I first came into contact with the C # language and then learned Win32 and MFC.

Some puzzling problems have been encountered during the learning process of transformation. For example, it takes a long time for system-level functions to be implemented in Win32 and MFC after being implemented in C. The following describes how to implement common functions in these three environments.

 

This article describes how to set system-level hotkeys:

 

C #

Step 1: declare two API functions at the class level,

/// Register the hotkey [Dllimport ( " User32.dll " )]
Private   Static   Extern   Bool Registerhotkey (intptr hwnd, Int ID, Uint Fsmodifiers, keys VK );

"User32.dll")]
Private Static Extern BoolUnregisterhotkey (intptr hwnd,IntID );/// Uninstall the hotkey
[Dllimport (

Note the introduction of namespace system. runtime. interopservices;

The preceding two function parameters are described as follows:

In the registerhotkey function,

Hwnd-basedProgramWindow handle. In the C # window, you can directly use the handle attribute to reference the window handle.

ID is the identifier of the hotkey. It is defined by ourselves. Because multiple hotkeys can be defined in a program, we need to use this field to differentiate them. For specific usage, see

When fsmodifiers is an activation hot key, whether or not it is used in combination with the system key, none: 0 alt: 1 CTRL: 2 shift: 4, and can also be used in combination with the or operation

VK is the hotkey to be defined. Keys in C # are included in the Keys enumeration.

In the unregisterhotkey function,

The hwnd and registerhotkey functions are the same. ID indicates the hotkey to be detached.

 

Step 2: register the hotkey in the window initialization, for exampleFor exampleRegisterhotkey (handle, 100, 0, keys. F9 );

Recognize some hotkeys as, 0 indicates that the system key is not used, keys. f9 indicates that this hot key is F9. If you want to press SHIFT + Ctrl + F9 at the same time, the third parameter should be 2 | 4 friends who are actually familiar with binary operations, 2 can be calculated immediately | 4 = 6

 

Step 3: Respond to the hotkey:

Rewrite the wndproc function of the form.CodeIs

 

Protected   Override   Void Wndproc ( Ref Message m ){ // The if condition is fixed
If (M. msg =   Zero X 312 ){ // This if is used to determine which hot key is used, and 100 corresponds to the aboveRegisterhotkeyThe second parameter in the function.
If (M. wparam. toint32 () =   100 ){ // Here is the Response Function
Dosomething ();
}
}
Base . Wndproc ( Ref M );
}

 

The wndproc () function is used to process Windows messages. It will be useful elsewhere!

 

Step 4: Call unregisterhotkey (handle, 100) at the end of the program to uninstall this hot key!

 

Win32

Step 1: In the int winapi winmain () function,

While (getmessage (& MSG, null, 0, 0 )){......}

Previously, registered hotkeys

Registerhotkey (hwnd, 100,0, vk_f9)

Vk_f9 is the macro defined in winuser. H. Other keys are also defined in a similar form. However, the numeric and numeric keys are directly in the form of '0' and 'A '.

Step 2: In while (getmessage (& MSG, null )){......}

Loop body, add code

 

// This is the response hotkey condition. from the definition of the wm_hotkey macro, it can be seen that the value isZero X 312, Which is consistent with the if condition in C #. If (Msg. Message = Wm_hotkey)
{ // The conditions here are used to determine the identifier of the hotkey. If (msg. wparam = 100) { // Call the function to be executed Dosomething (); }
}

 

Step 3: Call unregisterhotkey (hwnd, 100) to uninstall the hotkey after the while () loop ends.

 

MFC

MFC Program dialog box mode and document mode:

Dialog Box mode:

Step 1: add the wm_hotkey message response in the MFC form,

Void C hotkey DLG: onhotkey (uint nhotkeyid, uint nkey1, uint nkey2)
{ // The identifier used to judge the hotkey
If (Nhotkeyid =   100 )
{ // Response Function
Dosomething ();
}
Cdialog: onhotkey (nhotkeyid, nkey1, nkey2 );
}

 

Step 2: add the registration hotkey code in the initialization of the MFC form:

Registerhotkey (m_hwnd, 100,0, vk_f9 );

Note that the above field m_hwnd is a field in the cwnd class, which is controlled by MFC. We only need to reference it in a suitable place.

Step 3: Call unregisterhotkey (hwnd, 100) to uninstall the hotkey at the place where the form is destroyed.

Document mode:

In the document mode and dialog box mode, the situation is roughly the same. The difference is that the wm_onhotkey message is returned in the cview derived class, and the Code for registering the hotkey is added to the wm_create message of the cview derived class object, note that it cannot be placed in the constructor of this object, because the m_hwnd field has not been correctly initialized yet.

 

By now, you have mastered how to set system-level hotkeys in three environments. In my experience, the methods in Win32 and C # are roughly the same. On the basis of C #, I tried to implement this function in Win32, and the result was a success, however, there were some detours in MFC.

 

here we just use a simple example to compare the code differences in three environments. Other functions will be provided in the future. Please wait!

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.