Chapter 1 Programming windows Fifth Edition Reading Notes

Source: Internet
Author: User
Programming windows 5th Edition-Chapter 1 getting started

1. The first Windows program is as follows:

Code: select all
/*------------------------------------------------------------
    HelloMsg.cpp -- Display "Hello, Windows!" in
                   a message box
   Eric Zhang 2007
--------------------------------------------------------------*/

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               PSTR szCmdLine, int iCmdShow)
{
   MessageBox(NULL, TEXT("Hello, Windows!"), TEXT("HelloMsg"), 0);

   return 0;
}

There are a lot of things to explain in this program. First, include windows. h and windows. h are a collection of header files. They include a lot of necessary things, the most important and basic of which are:

The basic type definition of windef. h.
Winnt. h supports Unicode type definition.
WINBASE. h kernel function.
Winuser. h user interface function.
Wingdi. h graphic device interface function.

Since the beginning of Windows development, the core things have not changed, mainly including three: kernel, user, GDI, and kernel (in windows, this kernel is also called base, so there is WINBASE. h) is the core of windows, responsible for memory management, files, processes, etc. user is the user interface, such as the keyboard, and GDI is the graphical device interface.

Let's look at the code and see the winmain function, which is equivalent to the main function of a C program. The prototype of winmain is defined in WINBASE. H, as follows:

Code: select all
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nShowCmd);

Here we have made some changes. The first is the third parameter. We changed the type to pstr, which is actually char *, in the second chapter, we will give a detailed description of the characters in windows and the data type series of strings. Here we will change lpstr to pstr, in fact, the effect is similar, A pointer with LP represents a long pointer, which is a legacy product of a 16-bit Windows system. A 16-bit pointer with L represents 4 bytes, and now it is a 32-bit Windows system, so it doesn't matter if l is not added. We will see many data types and functions in 16-bit windows later. These are all legacy issues. In fact, when Windows is switched from 16-bit to 32-bit, the API data type expands greatly because it must be compatible with the original 16-bit program, A large number of data types and functions are added.

Nothing else changed, but we changed the variable name. For example, we changed the name of the lpcmdline to szcmdline. This is the name of the Hungary variable, which we have learned before, SZ indicates string terminaed with zero.

Then we can see the winapi in the winmain statement. This is a compilation parameter for the compiler, which is defined in windef. h:

# Define winapi _ stdcall

_ Stdcall, _ cdel, _ fastcall are commands that indicate the compiler, called call Convention. These commands tell the compiler when compiling a function, how to name a function (in a real binary program, the name of a function is not what we write now and needs to be changed ), how to import function parameters into and out of the stack (whether to push parameters from right to left into the stack or vice versa )....... I will introduce these things in a later article. In fact, these things are very important. Once an error is written, the program cannot be linked or the execution period may cause an unknown error, because many third-party libraries, his call convention may be different from ours. In order to call them, we must change our code to the same as ours. Otherwise, the parameters are different from those in the stack output stack, the retrieved parameters may be incorrect. Or, if our future Code contains C ++ code, the C ++ code has different call conventions for functions; in addition, different compilers may be different in many cases. Therefore, you still need to have a thorough understanding of call conventions.

Then we can see the parameters in the winmain function. The first parameter is the handle of the instance, which is actually a number used to identify our program instance, the second parameter is also a historical product. In early windows, if we run a program multiple times, multiple instances of the program will appear, our program can check this hprevinstance to determine whether our program has been launch, so that we can choose to do less work (such as some work that only needs to be done once ). However, this parameter has been deprecated. Therefore, for 32-bit windows, this parameter is always null.

The third parameter is the command line when the program is executed, from which we can get the command line options and parameter settings. The fourth parameter specifies the initial display mode of the program, whether to maximize the window or minimize the window.

Next we can see the MessageBox function. The first parameter is the window handle, which will be introduced in chapter 3. The text displayed in the body of the second parameter MessageBox dialog box; the third parameter is the text in the title bar of the dialog box. These two parameters are encapsulated by the text macro, in Chapter 2 Unicode, we will explain in detail why we want to do this and the specific content of the text macro. The fourth parameter indicates the MessageBox type and specifies the buttons in the MessageBox dialog box, defined in winuser. h usually has the following features:

Code: select all
#define MB_OK                       0x00000000L
#define MB_OKCANCEL                 0x00000001L
#define MB_ABORTRETRYIGNORE         0x00000002L
#define MB_YESNOCANCEL              0x00000003L
#define MB_YESNO                    0x00000004L
#define MB_RETRYCANCEL              0x00000005L

If multiple buttons are displayed in MessageBox, we can perform the preceding constant and the following constant | operation to specify a default button:

Code: select all
#define MB_DEFBUTTON1               0x00000000L
#define MB_DEFBUTTON2               0x00000100L
#define MB_DEFBUTTON3               0x00000200L
#define MB_DEFBUTTON4               0x00000300L

You can also specify the icon in the MessageBox dialog box in this parameter:

Code: select all
#define MB_ICONHAND                 0x00000010L
#define MB_ICONQUESTION             0x00000020L
#define MB_ICONEXCLAMATION          0x00000030L
#define MB_ICONASTERISK             0x00000040L

#define MB_ICONWARNING              MB_ICONEXCLAMATION
#define MB_ICONERROR                MB_ICONHAND
#define MB_ICONINFORMATION          MB_ICONASTERISK
#define MB_ICONSTOP                 MB_ICONHAND

By associating these constants with each other, you can use them in combination.

As for the return value of the MessageBox function, in this program, MessageBox returns the value 1, but more strictly speaking, it returns the idok. The idok is defined in winuser. h and is equal to 1. Based on other buttons displayed in the message box, the MessageBox function can also return idyes, IDNO, idcancel, idabort, idretry, or idignore.

After the program is explained, at the end of compilation, two pre-compilation condition variables (Preprocessor): Unicode and _ Unicode are defined in project properties, which enable Unicode support, as for how to implement it, the second chapter will detail Unicode.

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.