Windows Programming 02: The Eternal Hello World

Source: Internet
Author: User

Same as otherProgramming LanguageLike the textbooks, Charles Petzold also chooses "Hello World" as the classicProgramThe first Windows program example in design.

I call it the eternal "Hello World ".

Before learning how to write a Windows application, we need to first understand the dynamic link, the central idea of how Windows works.

1. Dynamic Link

The central idea of Windows's working principle is dynamic links.

What is dynamic link?

Windows has a set of functions. Applications call these functions to display text and graphics on the user interface and on the screen. Such calls do not integrate these functions like the machine code of the C language library functions.CodeYou can directly link to your application code by calling the dynamic link library.

These functions are implemented in the dynamic link library. The names of these dynamic link files are suffixed with. dllor. EXE.

How is dynamic link implemented?

When a Windows application is running, it interacts with a Windows application through a process called dynamic linking, each windows EXE file contains the dynamic link libraries used by it and the reference addresses of functions in the library. After a Windows application is loaded into the memory, function calls in the program are parsed into DLL function entry pointers, and these called functions are also loaded into the memory.

How does one compile a Windows program using the compiling environment?

When compiling a Windows program to generate an execution file, you need to link the special "import to Database" provided by the compiling environment ", these imported libraries contain the names and references of dynamic linked libraries encountered by all windows function calls. This information is used to compile Windows programs to build tables in the EXE file. When a program is loaded, Windows can use these tables to parse WINDOWS function calls.

 

2. parse Hello World

In the new source file "helloworld_msgbox.c", enter the followingSource code:

View code

1 # Include <windows. h> 2   3   Int  Winapi winmain (hinstance,  4   Hinstance hprevinstance,  5   Lpstr lpcmdline,  6                       Int  Nshowcmd)  7   {  8 MessageBox (null, text ( "  Hello, world!  " ), Text ( "  Helloworld_msgbox  "  ), Mb_ OK );  9       Return   0  ;  10 }

The effect after running is shown in 1.

Figure 1 helloworld

The following is a brief analysis of the source code from three aspects: file, program entry, and MessageBox.

2.1 header files

For Windows applications, windows. H is an extremely important header file because it contains several other Windows header files.

Common header files are as follows:

Windef. H basic data type definition

Winnt. h supports Unicode Type Definition

WINBASE. h kernel functions

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.

2.2 program entry

Windows program entry is winmain. The function is prototype:

 
1 IntWinapi winmain (hinstance,2 Hinstance hprevinstance,3 Lpstr lpcmdline,4IntNshowcmd)

The first hinstance parameter indicates the instance handle of the program, which uniquely identifies the currently running program. The second parameter is used to determine whether other instances are running (in 32-bit windows, this parameter is usually defined as null ). The third parameter is the command line used to run the program (lpstr indicates a long pointer to a string ). The fourth parameter nshowcmd is used to specify how the program should be displayed at first (normal display, full screen maximization, and minimized display on the taskbar ).

2.3messagebox Function

The MessageBox function is used to display short messages. The function is prototype:

 
1 IntMessageBox (hwnd, maid, maid );

The first parameter hwnd is usually a window handle. The second parameter lptext refers to the text information to be displayed in the information box. The third parameter "lpcaption" refers to the text information (that is, the title of the Information Box) to be displayed in the title bar of the information box ). The fourth parameter is used to set the button type in the information box.

2.3.1 button style

In the header file winuser. H, define some button types as follows:

 1   /*  2   * MessageBox () flags  3   */  4   # Define Mb_ OK 0x00000000l 5   # Define Mb_okcancel 0x00000001l 6   # Define Mb_abortretryignore 0x00000002l 7   # Define Mb_yesnocancel 0x00000003l 8   # Define Mb_yesno 0x00000004l 9   # Define Mb_retrycancel 0x00000005l 10   # If (Winver> = 0x0500) 11   # Define Mb_canceltrycontinue 0x00000006l 12   # Endif /* Winver> = 0x0500 */

Its Style 2 is shown in.

Figure 2 button style

2.3.2 default button

Also in the header file winuser. h defines the following macro definitions. By using bitwise OR (|), you can combine the above button style with the following macro definitions to describe which button is the default button.

1 # DefineMb_defbutton1 0x00000000l2 # DefineMb_defbutton2 0x00000100l3 # DefineMb_defbutton3 0x00000200l4 # If(Winver> = 0x0400)5 # DefineMb_defbutton4 0x00000300l6 # Endif/* Winver> = 0x0400 */

For example, when we use the button style mb_okcancel, there are two buttons: "OK" and "cancel. By default, the default button is "OK ". If you want to change the default button to "cancel", you can use the combination of mb_okcancel and mb_defbutton2.

The modified code is as follows:

View code

 1 # Include <windows. h> 2   3   Int  Winapi winmain (hinstance,  4   Hinstance hprevinstance,  5   Lpstr lpcmdline,  6                       Int  Nshowcmd)  7   { 8 MessageBox (null, text ( "  Hello, world!  " ), Text ( "  Helloworld_msgbox  " ), Mb_okcancel | Mb_defbutton2 );  9       Return   0  ;  10 }

2.3.3 icon displayed in the information box

To display some icons in the information box, you can use the following macro definitions. These macros are also defined in the header file winuser. H, as follows:

 
1 # DefineMb_iconhand 0x00000010l2 # DefineMb_iconquestion 0x00000020l3 # DefineMb_iconexclamation 0x00000030l4 # DefineMb_iconasterisk 0x00000040l

The four macros can also be used by bitwise OR (|) with the button style.

The four icons are shown in style 3.

Figure 3 icon Style

2.3.4 other models

In the header file winuser. h, there are some macro definitions about the information box, such as adding a "help" button.

 1   # Define Mb_applmodal 0x00000000l 2   # Define Mb_systemmodal 0x00001000l 3   # Define Mb_taskmodal 0x00002000l 4   # If (Winver> = 0x0400) 5   # Define Mb_help 0x00004000l // Help button  6   # Endif /* Winver> = 0x0400 */ 7   8   # Define Mb_nofocus 0x00008000l 9   # Define Mb_setforeground 0x0000000l 10   # Define Mb_default_shorttop_only 0x00020000l 11   12   # If (Winver> = 0x0400)13   # Define Mb_topmost 0x00040000l 14   # Define Mb_right 0x00080000l 15   # Define Mb_rtlreading 0x00100000l 16   # Endif /* Winver> = 0x0400 */

 

 

Related blogs:

My first Windows program Http://www.cnblogs.com/mr-wid/archive/2012/10/06/2713249.html

 

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.