/*------------------------------------------------------------------HelloMsg.c -- Displays "Hello, Windows 98!" in a message box(c) Charles Petzold, 1998--------------------------------------------------------------------*/#include <windows.h>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0); return 0 ;}
Almost every Windows program written in C/C ++ will use include to introduce the "windows. H" header file at the beginning. Windows. H is the main file containing incoming content,
It contains other Windows header files. Some of these header files also contain other header files. The most important and basic of these header files are:
Windef. H basic schema definition.
Winnt. h supports Unicode type definition.
WINBASE. h kernel function.
Winuser. h user interface function.
Wingdi. h graphic device interface function.
These header files define all windows data types, function calls, data structures, and constant identifiers, which are an important part of Windows files.
Every C/C ++ program has a program entry (main function), and Windows program entry point: winmain, which always appears like this:
int WINAPI WinMain ( HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow)
Winmain has the following declaration in WINBASE. h:
intWINAPIWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd );
The third parameter is in WINBASE. H is defined as lpstr, and pstr is used in this example. both data types are defined in winnt. h, as a pointer to the string. LP prefix indicates "Long Pointer", which is written in 16-bit windows.
The winmain function is declared to return an int value. The winapi identifier is defined in windef. h. The statement is as follows:
#define WINAPI __stdcall
This statement specifies a call convention, including how to generate a mechanical code to place a function call parameter in the stack. Many WINDOWS function calls are declared as winapi.
The 1st parameters in winmain are called "execution object handle 」. in Windows programming, a handle is only a number used by an application to identify something. in this case, the handle uniquely identifies the program and needs to be used as a parameter in other Windows function calls.
In earlier versions of Windows, when the same program was run multiple times at the same time, multiple execution entities (multiple instances) of the program were created )」. all implementing entity sharing programs and read-only memory for the same application (resources such as menus and dialog box templates ). by checking the hprevinstance parameter, the program can determine whether its other execution entities are running. Then, it can skip some complicated work and move some data from the previous execution entities to its own data region, in 32-bit windows, this concept has been abandoned, so the 2nd parameters passed to winmain are always null (defined as 0 ).
The 3rd parameter in winmain is the command column used to execute the program. For example, some Windows applications use it to load files into memory when the program starts.
The 4th parameters in winmain indicate the method in which the program is initially displayed, which can be normally or fully filled with the entire screen, or minimized in the work column.
The MessageBox function is used to display short messages. Although the small window displayed in MessageBox does not have any function, it is actually considered as a dialog box.
The first parameter of MessageBox is usually a window handle.
The 2nd parameters are strings displayed in the message body.
The 3rd parameters are strings that appear in the title column of the message box. In the example program, each of these character strings is encapsulated in a text macro.
The 4th parameters of MessageBox are used to display buttons in the dialog box. It can be a group of constants defined in winuser. H that have previously started with MB _. The following content is available:
#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 4th parameters are set to 0 in the example program, only the "OK" button is displayed. you can use the OR (|) operator to combine a constant shown above with a constant representing the inner button:
#define MB_DEFBUTTON1 0x00000000L#define MB_DEFBUTTON2 0x00000100L#define MB_DEFBUTTON3 0x00000200L#define MB_DEFBUTTON4 0x00000300L
You can also use a constant to indicate the icon appearance in the message box:
#define MB_ICONHAND 0x00000010L#define MB_ICONQUESTION 0x00000020L#define MB_ICONEXCLAMATION 0x00000030L#define MB_ICONASTERISK 0x00000040L
Some of these icons have alternative names:
#define MB_ICONWARNING MB_ICONEXCLAMATION#define MB_ICONERROR MB_ICONHAND#define MB_ICONINFORMATION MB_ICONASTERISK#define MB_ICONSTOP MB_ICONHAND
In the example 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. The MessageBox function can return idyes, IDNO, idcancel, idabort,
Idretry or idignore.