Windows programming (c) Chapter 1th Introduction to SDK

Source: Internet
Author: User
Tags message queue

First, study background

Now what we're going to learn is WINDOWS32 programming, unlike DOS, the goal is to have a "window" with the functionality you need. This "window" is always in between the operating system, through a "message queue" of something called the data transmission. So handling all sorts of messages becomes the core of Win32 programming.

SDK programming is relative to MFC, that is, using C and C + + to invoke Windowsapi,api is the operating system, other languages (such as VB) can also be called. As a result, some people used to call this programming API programming. Of course, there are some extensions to C or C + +, the so-called extension, is to add a lot of macro definitions, templates, let you look at the surprise, I do not know "where sacred" upside down what the avatar.

If you do not have C or C + +, please learn C and C + + language, otherwise, you work hard also equals to see a movie, perhaps you understand but you certainly can not play the film. Before Learning window programming, you need to know several concepts.

    1. Example (instance): This is the content of C + +, "class" is equivalent to the data type of the struct, the "class" type to define a variable (if it is a pointer, it is assumed that the space has been obtained), this variable is the "class" of the "instance." It is one thing with variables in the C language, but only variables defined with "class" are called instances.
    2. Handle (HANDLE): This is the content of the C language, like the file handle, which is actually an integer that identifies which instance, that is, the identifier of the instance. This instance can be manipulated by a handle.
    3. Callback (CALLBACK): This is actually a function pointer in the C language, and the function pointer is usually used when a large project is framed. That is, you design what to get, the interface is defined well, how to implement completely to others, others just pass the function name to you OK. The function name is the address, but the pointer to this address is somewhat different from the normal pointer on the definition. There are detailed explanations and examples of "function pointers" in the related tutorials in C.
second, build the programming environment

The older vc++6.0 can be used as well as the newer visual Studio 2017, but vs is large in volume, consumes more system resources, and many features are difficult to use at this stage, so this article uses the older VC.

Construction of 2.1 VC development environment

(1) After opening the VC, click on the menu bar "file (F)"--"New (N)"--"project"--win32 application, fill in the project name, this article takes test as an example, click "Finish" to end the creation, 1-1 and 1-2 are shown;

Figure 1-1 New Project 1

Figure 1-2 New Project 2

(2) Add the source code file in the project, click "File (F)" In the menu bar--"New (N)"--"file"--c/c++ Header file, fill in the name, tick add to project, 1-3 as shown.

Figure 1-3 Creating a source code file

2.2 Visual Studio development Environment Building

(1) "File"--"new"--"project"--"Visual C + +"-"Win32 project";

Figure 1-4 vs Building the programming environment 1

Figure 1-5 vs Building the programming environment 2

Figure 1-6 vs Building the programming environment 3

(2) Create another console application, the following steps with 2.1, no longer repeat.

Third, the first procedure

3.1 MessageBox function

The MessageBox function is used to display a modal dialog box that contains a system icon, a button, and a short message that is applied to the application, such as status or error information. The function is described in detail below:

1 int WINAPI MessageBox (2  _in_opt_  hwnd hwnd,3  _in_opt_  LPCTSTR Lptext,4  _in_opt_  lpctstr lpcaption,5  _in_      UINT Utype 6 );

Note:_in_ indicates that the parameter is input, and _opt_ indicates that the parameter is an optional parameter.

The meaning of the parameter is shown in table 1-1:

Table 1-1 MessageBox function parameter meaning

Parameters Meaning
Hwnd

1, the parent window handle of the message box;

2. If this parameter is NULL, the message box does not have a parent window.

Lptext contents of the message box
Lpcaption The title of the message box
Utype

1. Specify a set of bit flags that determine the content and behavior of the dialog box;

2. This parameter can be combined with the following flags or flags to display the buttons and icons in the message box.

Table 1-2 utype Parameter definition Resolution

Button Meaning
Mb_ok Default value, show "Confirm" button
Mb_yesno Show "Yes" and "No" two buttons
Mb_abortretryignore Show abort , retry , and skip three buttons
Mb_yesnocancel Show "Yes" , "No" , "Cancel" three buttons
Mb_retrycancel Show "Retry" and "Cancel" two buttons
Mb_okcancel Show "Confirm","Cancel" two buttons

Others can be found here.

The MessageBox has a return value, and the return value is the button clicked by the user.

1 #defineIDOK 12 #defineIDCANCEL 23 #defineIdabort 34 #defineIdretry 45 #defineIdignore 56 #defineIdyes 67 #defineIdno 7

3.2 WinMain () function

The WinMain () function is the entry function for a window program, in which you can invoke various API functions to accomplish your goal. Similar to main () in the normal C language source file, WinMain is the entry point in Windows Programming. The specific steps are as follows:

① is generally called the RegisterClassEx () function to use the current window handle to the operating system application (or called login) will create a window, after the successful application,② call CreateWindowEx () The function creates a Window object, which is just an appearance, and③ also calls the ShowWindow () function to set the initial representation, that is, maximum or minimum or normal. ④ finally calls the UpdateWindow () function to send the WM_PAINT message to the window to draw the contents of the window. After the window is created, this is a "still" window,⑤ so also add the message loop at the end of the WinMain () function, and finally return. After the WinMain () function is finished,⑥ also writes a "window message processing" function. The call to a bunch of API functions above may be a bit faint, but those are all fixed and basically not programmed. After you understand, just modify a small number of parameters, the real programming is the "window message processing" function.

Let's start with a simple way of not creating a window, but just calling a common dialog box. The main idea is to understand the WinMain () function.

1#include <windows.h>2 3 intApientry WinMain (HInstance hinstance,//Current Window instance handle4HInstance hPrevInstance,//Previous instance handle, NULL under Win325LPSTR lpCmdLine,//command line parameter number character6                      intnCmdShow//How the window is displayed7                     )8 {9MessageBox (Null,text ("Hello world!"), TEXT ("my program"), MB_OK);//Common dialog BoxTen  One     return 0;//function return value A}
   
Program Run Effect:

Figure 1-4 Program Run effect

Code Explanation:
The 1th behavior joins the Windows programming header file, the second line leaves a row, makes the header file declaration and the code line to distinguish the level and the space is a good programming habit, enhances the code readability.
The 3–7 line is required to invoke the message box. The following is a simple explanation, the beginner knows that it is necessary to invoke the message box, the reader does not understand and does not matter.

(1) Apientry is not a return type, it is a macro definition prepared for the compiler that tells the compiler to compile the current function into a long call that can be used by a 64-bit machine. Why do you need a switch like this? Visualc++ is compatible with C, C + +, 16-bit versions of Windows, and is ready to be extended to 64-bit machines in the future. Although it is the same macro definition as callback, it is still slightly different at compile time.

(2) the WinMain () function is in the. NET becomes _tWinMain (), which is also defined as a macro definition for compatibility.

(3) HInstance is an instance handle, something that begins with H or H, such as HDC, is a handle whose value is an integer. If you are hard-written int, the compiler will not be clear, so the compiler error.

(4) LPSTR is a string pointer of Unicode type.

(5) HInstance is the instance handle of the current window, hprevinstance is used on a 16-bit machine to identify whether the current window is repeatedly opened, and when hPrevInstance is not null for the previous window instance handle, On a 32-bit machine, hPrevInstance constant is null.

(6) lpCmdLine is a parameter from the command line.

(7) nCmdShow is the initial display mode, max/min/normal/hidden and so on.

(8) The MessageBox () function is a WIN32 API function that invokes a common dialog box. This is not the window programming we are going to learn.

A 9-row message box Returns an integer value that indicates which button the user clicked. If you want to get this value, you can modify the above code as follows.

1#include <windows.h>2 3 intApientry WinMain (HInstance hinstance,//Current Window instance handle4HInstance hPrevInstance,//Previous instance handle, NULL under Win325LPSTR lpCmdLine,//command line parameter number character6                      intnCmdShow//How the window is displayed7                     )8 {9    intRet=messagebox (Null,text ("are you over 18 years old? "), TEXT ("Determine whether adult"), Mb_yesno);//Common dialog BoxTen  One    if(ret==idyes) A { -MessageBox (Null,text ("you're a grown man! "), TEXT ("Judging Results"), MB_OK); - } the Else - { -MessageBox (Null,text ("you haven't grown up yet! "), TEXT ("Judging Results"), MB_OK); - } +  -     return 0;//function return value +}

If you want to display the contents of Table 1-2 and table 1-3 at the same time, you can use the | (or operator), for example:

MessageBox (NULL, Text ("HelloWorld"), Text ("hello"), Mb_ okcancel| Mb_iconquestion);

Windows programming (c) Chapter 1th Introduction to SDK

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.