One-day Windows API training (2) using application handles

Source: Internet
Author: User
From the above section, we can see that _ twinmain is the entry function of the application. Here we use its macro, which is defined in the tchar. h header file. Why do we need to define a macro like this? Because Windows applications must adapt to Unicode and previous single-character applications, the definitions of Windows APIs are different, as shown below:
Unicode definition:
# DEFINE _ twinmain wwinmain
Single Character definition:
# DEFINE _ twinmain winmain
After such macro definition, function interfaces with different character widths can be adapted. Since I use Unicode compilation, we use the wwinmain function definition here. Next we will find its definition as follows:
Int
Winapi
Wwinmain (
Hinstance,
Hinstance hprevinstance,
Lpwstr lpcmdline,
Int nshowcmd
);
Here we need to explain in detail the wwinmain Parameter Function, which has four parameters.
HinstanceIs the instance handle of the current application, which is generally used to differentiate the use of different resources.
HprevinstanceIt is the handle used by Win98 in the past, and is null in the operating system after Win2000.
LpcmdlineIt is a command line parameter. For example, if you run a program in the Windows Start Menu and add the parameters to it, it will be passed to the application, which will be discussed in detail later.
NshowcmdIs the display mode of the window, such as maximize display, minimize display, or normal display.
 
When running a program in windows, the wwinmain function is called through the startup code in the Runtime Library. It is called in the startup file as follows:
# Ifdef wprflag
Mainret = wwinmain (
# Else/* wprflag */
Mainret = winmain (
# Endif/* wprflag */
(Hinstance) & __ imagebase,
Null,
Lpszcommandline,
Startupinfo. dwflags & startf_useshowwindow
? Startupinfo. wshowwindow
: Sw_showdefault
);
This is the value that the operating system passes to the application. Now we will demonstrate using the first parameter hinstance.
See the following example:
#001 # include "stdafx. H"
#002 # include "first. H"
#007 //
#008 int apientry _ twinmain (hinstance,
#009 hinstance hprevinstance,
#010 lptstr lpcmdline,
#011 int ncmdshow)
#012 {
#013 unreferenced_parameter (hprevinstance );
#014 unreferenced_parameter (lpcmdline );
#015 unreferenced_parameter (ncmdshow );
#016
#017 // use the application handle
#018 const int maxsize_appbuf = 256;
#019 tchar wapptile [maxsize_appbuf];
#020 loadstring (hinstance, ids_app_title, wapptile, maxsize_appbuf );
#021
#022 // obtain the desktop handle.
#023 hwnd = getdomaintopwindow ();
#024
#025 // display a message line.
#026 MessageBox (hwnd, _ T ("first application"), wapptile, mb_ OK );
#027
#028 //
#029 return 0;
#030}
This example was modified on the basis of the previous section, mainly adding the application instance handle. Define a buffer for saving the application title in row 19th, and then call the function in row 20th. LoadstringWhen a string is loaded from an application resource, its first parameter uses the hinstance handle. Therefore, the application handle is the unique identifier of the Program on the resource.

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.