One-day training of Windows APIs (10) loadaccelerators Function

Source: Internet
Author: User
When users use software, some features are often the most commonly used. As developers, you need to make users feel that the software is easy to use. In this way, you need to use the most commonly used functions in the most convenient and convenient way to improve production efficiency. In this regard, Microsoft is doing its best. For example, there is a Windows key on the keyboard. Many functions of Windows can combine this key with other keys to form shortcuts, improving ease of use. Now let's introduce how to make your program easy to use, that is, using shortcuts.
The loadaccelerators function is declared as follows:
Winuserapi
Haccel
Winapi
Loadacceleratorsa (
_ In_opt hinstance,
_ In lpcstr lptablename );
Winuserapi
Haccel
Winapi
Loadacceleratorsw (
_ In_opt hinstance,
_ In lpcwstr lptablename );
# Ifdef Unicode
# Define loadaccelerators loadacceleratorsw
# Else
# Define loadaccelerators loadacceleratorsa
# Endif //! Unicode

HinstanceIs the application instance handle, used to find the shortcut key definition from the program resource file.
LptablenameIs the definition table of the shortcut key.

An example of calling this function is as follows:
#001 // main program entry
#002 //
#003 // Cai junsheng 2007/07/12
#004 //
#005 int apientry _ twinmain (hinstance,
#006 hinstance hprevinstance,
#007 lptstr lpcmdline,
#008 int ncmdshow)
#009 {
#010 unreferenced_parameter (hprevinstance );
#011 unreferenced_parameter (lpcmdline );
#012
#013 //
#014 MSG;
#015 haccel hacceltable;
#016
#017 // load the global string.
#018 loadstring (hinstance, ids_app_title, sztitle, max_loadstring );
#019 loadstring (hinstance, idc_testwin, szwindowclass, max_loadstring );
#020 myregisterclass (hinstance );
#021
#022 // application initialization:
#023 if (! Initinstance (hinstance, ncmdshow ))
#024 {
#025 return false;
#026}
#027
#028 hacceltable = loadaccelerators (hinstance, makeintresource (idc_testwin ));
#029
#030 // message loop:
#031 while (getmessage (& MSG, null, 0, 0 ))
#032 {
#033 if (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
#034 {
#035 translatemessage (& MSG );
#036 dispatchmessage (& MSG );
#037}
#038}
#039
#040 return (INT) msg. wparam;
#041}

Row 3 calls the loadaccelerators function to load shortcuts from resources.

Now, you have learned about and used the loadaccelerators function. But there is another question: how are the shortcut key resources defined? Well, let's take a look at its definition below:
#001 ////////////////////////////////////// ///////////////////////////////////////
#002 //
#003 // accelerator
#004 //
#005
#006 idc_testwin Accelerators
#007 begin
#008 "? ", Idm_about, ASCII, ALT
#009 ", idm_about, ASCII, ALT
#010 end
#011

This is copied from the resource file testwin. RC. Idc_testwin is the name of the shortcut key table, accelerators is the key word defined by the shortcut key, begin is the start of the shortcut key, and end is the end of the shortcut key.
The following shortcut key definition Syntax:
Acctablename accelerators [Optional-statements] {event, idvalue, [type] [Options]...}
[Optional-statements] is an optional option.
An event is a required content. It is a defined key code or an ascii code of the key.
Idvalue is the id command for the shortcut key response.
Type is the type selection.
Set Alt + 'in rows 8th and 9th above '? 'Or Alt +'/'as the shortcut key. When the user presses Alt + '? 'Or Alt +'/'dialog box appears.

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.