For a background program, you often need to add a hotkey. At this time, since the application is not at the desktop, the messages defined in the standard MFC are no longer useful. The general approach is:
After you have finished the necessary operations in INIT, please register a system hotkey, and release it after the program exits.
The system hotkey registration is not difficult to write the function code for the system hotkey response to the background service program according to the foregoing introduction. First, through the function globalfindatom () query whether the global atoms corresponding to this service program have existing in the global atomic table, if discovered, this service already exists in the system, the program exits. If you do not find it, add a string to the global atomic table via the GlobalAdDatom () function and get a unique atom that uniquely identifies this string. The above two function prototypes are:
Atom GlobalFindatom (LPCTSTR LPSTRING); Atom GlobalAddatom (LPCTSTR LPSTRING);
The input parameter is a string describing the atom, and if the globalfindatom () finds the specified string from the global working table, the atom corresponding to this string is returned to 0. GlobalAddatom () If the creation is successful, a newly created atom will be returned.
Next, in order to capture the system hotkey during the program operation, you need to define a system-wide hotkey to define a system range. The function is as follows:
Bool RegisterhotKey (HWND HWND, // Receive Hot Key Responsive Window Handle INT ID, / / Hot Key Logo UINT FSMODIFIERS, // Control Key Sign UINT VK // Virtual Key Value);
Among them, the hotkey identification ID must be a global unique value range between 0xC000 to 0xFFFF, in order to avoid the possible thermal bond conflict, usually introduced as a parameter, and GlobalAdDatom () returns the value of GlobalAddataom () The range of allowable scope of the ID parameters is exactly consistent. The parameter fsmodifiers defines the control key combination of system hot key messages with the virtual key value VK, such as MOD_ALT, MOD_CONTROL, MOD_SHIFT, and MOD_WIN, and the like. In this example, the system hotkey to be set is Alt Ctrl R, so the parameters fsmodifiers and VK are set to mod_alt | mod_control and vk_r, respectively. Registration implementation methods for system hotkeys can be organized as follows:
/ / Get the current window handle hwnd handle = getSafehWnd (); // Looking for the atomic atomic atom exists in the atomic list if ("Hotkey") == 0) {// If there is no list of atoms, create a Atom ID = Globaladdatom ("Hotkey"); // Register the global hotkey Ctrl Alt RregisterhotKey (HANDLE, ID, Control Alt, R);} Else // If Hotkey already exists in the atomic list, the termination program is running postquitMessage (0);