Windows Programming _ chap01 _ start _ Study Notes

Source: Internet
Author: User
Tags microsoft c
Windows Programming _ chap01 _ start _ Study Notes

--By: neicole (2013.05.21)

 

01. Opening

 

Today is 2013.05.20. I started to learn the book "windows programming", during which I will learn through msdn and network materials. The first chapter of the book briefly introduces the history of windows, briefly introduces the concept of dynamic link library, describes the preparation of Windows programming, and the most important first Windows program. This learning note mainly translates msdn to learn to use two basic windowsapi functions.

 

02. Important header files of windowsapi (Dynamic Link Library)

 

02.01 kernel WINBASE. H (kernel32.dll) is responsible for five basic tasks of the Operating System: process management, storage management, device management, file management, and operating system interfaces.

02.02 user winuser. H (user32.dll) user interface, responsible for all window management.

02.03 Graphics Device Interface (GDI) wingdi. H (gdi32.dll) is responsible for displaying text and graphics on the screen or printer.

02.04 basic data type definition windef. h

02.05 support for Unicode type definition winnt. h

 

03. My first Windows program

 

03.01 effect demonstration

 

Source code 03.02

/*** File: hahaworld. cpp * Description: the first program implemented by windowsapi displays a MessageBox. * Ide: vs2010 SP1 * Producer: neicole * Contact: http://blog.csdn.net/neicole **/# include <windows. h> int winapi winmain (hinstance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {MessageBox (null, text ("Haha world! "), Text (" hahamsg "), mb_defbutton1 | mb_yesno); Return 0 ;}

 

04. Program explanation

 

04.01 winmain Function

The winmain function is used as the windows program portal. After reading msdn, I got an introduction to winmain (I personally understand the translation below. If you have any mistakes, please point out that you want to correct them ):

Every windowsprogram implements des an entry-point function that is named either winmain orwwinmain.

An empty windows program contains an entry function winmain or wwinmain.

 

04.01.01 parameter Introduction

Hinstance is something called a "handle to an instance" or "handle to a module. "The operating system uses this value to identify the executable (exe) when it is loaded in memory. the instance handle is needed for certain Windows functions-for example,
To load icons or bitmaps.

Hinstance is called "instance handle" or "module handle ". The operating system uses the value of this handle to define the components loaded into the memory. You need to use Instance handles to use window functions such as loading icons or bitmaps.
Hprevinstance has no meaning. It was used in 16-bit windows, but is now always zero.
Hprevinstance has no meaning. It was previously used in a 16-bit system, but is usually zero now.
Pcmdline contains the command-line arguments as a unicode string.
Pcmdline contains the program's (UNICODE programming string) command line parameters.
Ncmdshow is a flag that says whether the main application window will be minimized, maximized, or shown normally.
Ncmdshow is a sign that describes how the main application window is displayed, such as minimizing, maximizing, or displaying normally.

04.01.02 Return Value

The function returns an int value. The return value is not used by the operating system, but you can use the return value to convey a status code to some other program that you write.
This function returns an int value. This return value is not used by the operating system, but you can use the return value to convey a status code to some programs you write.
Winapi is the calling convention. A calling convention defines how a function has es parameters from the caller. for example, it defines the order that parameters appear on the stack. just make sure to declare your wwinmain function as shown.
Winapi is a call convention. Defines how the function receives parameters. For example, it defines the parameter pressure stack sequence (from right to left), so that you can make sure that your wwinmain function can be properly displayed.

 

04.01.03 remarks

The winmain function is identical to wwinmain, Except t the command-line arguments are passed as an ANSI string. the Unicode version is preferred. you can use the ANSI winmain function even if you compile your program as Unicode. to get a unicode copy
Of the command-line arguments, call the getcommandline function. this function returns all of the arguments in a single string. if you want the arguments as an argv-style array, pass this string to commandlinetoargvw.
The winmain function and wwinmain function are identical except that the command line pcmdline parameter uses ANSI strings. The Unicode version is preferred. When your program is compiled with Unicode, you can also use ANSI command line parameters. Obtain a copy of the Unicode command line and call the getcommandline function. This function returns these parameters to a unique string. If you want these parameters to be an array of command line parameter styles, use this string as the parameter to execute the CommandLine function.
How does the compiler know to invoke wwinmain instead of the standard main function? What actually happens is that the Microsoft C Runtime Library (CRT) provides an implementation of main that CILS either winmain or wwinmain.
How does the compiler know how to call wwinmain to replace the standard main function? This is because the standard Runtime Library (CRT) of Microsoft C provides the function call interface of winmain or wwinmain.
Note: The CRT does some additional work inside main. for example, any static initializers are called before wwinmain. although you can tell the linker to use a different entry-point function, use the default if you link to the CRT. otherwise, the CRT Initialization
Code will be skipped, with unpredictable results. (For example, global objects will not be initialized correctly .)
Note that the standard Runtime Library will perform some additional operations in main. For example, some static initialization objects will be initialized before wwinmain is executed. If you connect to the CRT, you will use the default entry function, even if you can tell the keyboard operator to use different entry functions. Otherwise, the CRT initialization object code will be skipped and unpredictable results may occur. (For example, all variables cannot be correctly initialized .)

 
04.02 message function

Int MessageBox (hwnd, maid );
This is a function that is often used in Windows programming, just as cout is often used in Win32 console programming.

 

04.02.01 parameter Introduction

Hwnd [in] handle to the owner window of the message box to be created. If this parameter is null, the message box has no owner window.
The parent window handle of the message box. If this parameter is null, the message box does not have a parent window.
Lptext [in] pointer to a null-terminated string that contains the message to be displayed.
Pointer to a string that ends with null and contains the message to be displayed.
Lpcaption [in] pointer to a null-terminated string that contains the dialog box title. If this parameter is null, the default title error is used.
Pointer to a string ending with NULL for the dialog box title. If the parameter is null, the default title error will be used.
Utype [in] specifies the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
Specifies the Bit Flag Set of the content and behavior of the dialog box. This parameter can be a combination of the following flag groups. (Not listed here)

 

04.02.02 Return Value

If a message box has a Cancel button, the function returns the idcancel value if either the ESC key is pressed or the cancel button is selected. if the message box has no cancel button, pressing ESC has no effect.
If there is a Cancel button in the message box, if the ESC key is pressed or the cancel button is selected, the function returns the idcancel value. If there is no message button in the message box, pressing the ESC key will not produce any effect.
If the function fails, the return value is zero. To get extended error information, call getlasterror.
If the function fails, the return value is 0. Call getlasterror to obtain more detailed error information.
If the function succeeds, the return value is one of the following menu-item values.
If the function succeeds, the return value will be one of the following lists. (Not listed here)

 

04.02.03 remarks

Adding two right-to-left marks (rlms), represented by Unicode formatting character U + 200f, in the beginning of a MessageBox display string is interpreted by the Win32 MessageBox rendering engine so as to cause the reading order of the MessageBox to be
Rendered as right-to-left (RTL ).
Two signs (rlms) from right to left are added as unicode format characters U + 200f. In the start message box, the strings are interpreted by the rendering engine in the Win32 pop-up window, display the reading sequence of the dialog box from right to left (RTL ).
When you use a system-modal message box to indicate that the system is low on memory, the strings pointed to by the lptext and lpcaption parameters shocould not be taken from a resource file because an attempt to load the resource may fail.
When you use a system modal message box to indicate that the system is low memory, the lptext and lpcaption parameters pointed to by this string cannot come from the resource file, because the attempt to load resources may fail.
If you create a message box while a dialog box is present, use a handle to the dialog box as the hwnd parameter. the hwnd parameter shocould not identify a child window, such as a control in a dialog box.
If you create a message box when a dialog box appears, use the handle of the dialog box as the parameter. This handle parameter cannot be used as a subwindow, just as the control in the dialog box.

 

05. Terminator

The first Windows API program came out ~ Hahaworld is used to replace helloworld. This is because Haha seems more dynamic. Now it's just the beginning. For Windows API programming, more exciting knowledge is coming soon ~

 

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.