Windows Programming [23]-Shortcut Key Resources

Source: Internet
Author: User
Tip:

1. Because the shortcut key is added, the basic code in this example is different from the previous one, because the shortcut key resource must be loaded before the form initialization, And the shortcut key message must be intercepted for further processing.

2. I have been familiar with many topics about shortcut keys before, but these are not the essence:
Register System-level hotkeys
List of all optional shortcut keys
Shortcut Key setting control
How to record shortcuts

3. I used to think that the shortcut key is just to intercept keyboard messages. Now it seems that it is too simple to think about. It is easy to say that keyboard messages require focus, the shortcut key is only for the main window.

4. Create a shortcut table or a shortcut resource before using the shortcut key. In this example, the latter is used.
The shortcut key is generally used to execute the menu function. How is it attached to the menu? Simple: You just need to specify the same ID for each shortcut key and each menu item!

5. In this example, after the window is created, load the shortcut key resource with loadaccelerators and return the shortcut key resource handle HACC;
After getmessage retrieves a message from the message queue, use translateaccelerator to check whether the message is a shortcut message. if yes, translateaccelerator converts the shortcut message to a menu message (wm_command, or system menu message: wm_syscommand), and then sends it directly to the window. If not, continue the previous message transmission.
In fact, the essence of the shortcut key message is also the keyboard message, but the translateaccelerator will check whether the Keyboard Message combination is our defined shortcut key. but when we execute the shortcut key, the focus may not be in the main window. Why is it that the keyboard messages in other windows are transferred to the main window?
If the second parameter of the getmessage function is 0, it will retrieve all window messages in the program. After the translateaccelerator receives the message, no matter which window the message comes from, switch the window handle in the message package to the main window handle and then pass it.

The shortcut key resource file should be explained below the resource file...

In this example:


Resource file (testres. Rc) used in this example ):

Mymenu1 menuexbegin popup "in menuitem" menu _ 1 \ t SHIFT + 1 ", 101 menuitem" menu _ 2 \ t Ctrl + 2 ", 102 menuitem "menu _ 3 \ t Alt + 3", 103 menuitem "menu _ 4 \ t SHIFT + CTRL + ALT + 4 ", 104 menuitem "menu _ 5 \ t 5", 105 menuitem "menu _ 6 \ t F5", 106 menuitem "menu _ 7 \ t Ctrl + ", 107 menuitem "menu _ 8 \ t Alt + A", 108 endmyaccel acceleratorsbegin "1", 101, primary key, shift, noinvert "2", 102, primary key, control, noinvert "3", 103, primary key, ALT, noinvert "4", 104, primary key, shift, control, ALT, noinvert "5", 105, primary key, noinvert, noinvert vk_f5, 106, primary key, noinvert, noinvert "A", 107, primary key, control "A", 108, ASCII, ALT end
 

Note:

1. Keyword of the shortcut key resource file: accelerators. You need to give a name to loadaccelerators in the program. Here is: myaccel.

2. There can be five sets of parameters in the shortcut key resource file. The "noinvert" indicates that the top menu of the activated menu does not Flash (but I did not see it ).
The control keys are shift, control, and ALT.
The preceding two options are available: primary key, ASCII, and primary key, indicating that the shortcut key should be described with a virtual key code; ASCII indicates that the shortcut key is described with an ascii code. for example, vk_f5 is a typical virtual key code. however, there is no virtual key code corresponding to the regular and hidden keys. For numbers (non-keypad numbers), there is no difference between the two. Note: when the primary key is used, uppercase letters and ASCII letters must be used.

3. The second parameter must correspond to the menu identifier.

4. In addition, the menu resource is also a little different from the previous one: the corresponding shortcut keys are described in text after \ t (Tab meaning), but this is only the display effect, does it affect the function.

This example code file:
Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages; {work required to receive the wm_command message} procedure oncommand (H: hwnd; wparam: integer); var W: word; arr: array [byte] of char; begin W: = loword (wparam); Case W of 101 .. 108: Begin getmenustring (getmenu (H), W, arr, length (ARR), mf_bycommand); setwindowtext (H, arr); end; function wndproc (WND: hwnd; MSG: uint; wparam: integer; lparam: integer): integer; stdcall; begin result: = 0; Case MSG of wm_command: oncommand (WND, wparam ); {call oncommand process after wm_command message received} wm_destroy: postquitmessage (0); else result: = defwindowproc (WND, MSG, wparam, lparam); end; function regmywndclass: Boolean; vaR CLS: twndclass; begin Cls. style: = cs_hredraw or cs_vredraw; Cls. lpfnwndproc: = @ wndproc; Cls. cbclsextra: = 0; Cls. cbwndextra: = 0; Cls. hinstance: = hinstance; Cls. hicon: = 0; Cls. hcursor: = loadcursor (0, idc_arrow); Cls. hbrbackground: = hbrush (color_window + 1); Cls. lpszmenuname: = 'mymenu1'; Cls. lpszclassname: = 'mywnd'; Result: = registerclass (CLS) 0; end; {program entry} const tit = 'new form'; Ws = ws_overlappedwindow; X = 100; y = 100; W = 300; H = 180; var hwnd: thandle; MSG: tmsg; HACC: haccel; {define the shortcut key resource handle} begin regmywndclass; hwnd: = createwindow ('mywnd', tit, WS, X, Y, W, H, 0, 0, hinstance, nil); showwindow (hwnd, sw_shownormal); updatewindow (hwnd ); HACC: = loadaccelerators (hinstance, 'myaccel'); {load shortcut key resource} while (getmessage (MSG, 0, 0, 0) do begin if (translateaccelerator (hwnd, HACC, MSG) = 0) Then {intercept and process the shortcut key message} begin translatemessage (MSG); dispatchmessage (MSG); end.
 
Related Article

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.