WIN32 Api_ the first class, Simple WIN32 program creation

Source: Internet
Author: User

Because the previous study has been confined to the language basis of the part, but also to have the form of the program feel very interested, of course, there are many students and I have the same doubts, why I write the program and I use the program is not the same, I wrote the program is a black box, And I usually use on the computer program is a beautiful interface. I will be a learner to express my understanding of these, if there is an inappropriate welcome advice.

As you learn, you also learn about the relationships between the black box and the multifarious forms, and know what is called GUI (Graphic User Interface). Also, as most learners come into contact with the MFC framework, there is also the Qt graphics library and WPF in the current mainstream. NET Framework. But after a little more understanding, but also understand the lower level, and such as MFC and WPF is only the API call and encapsulation, if it is to learn, do not pursue development efficiency, then use WIN32 The API can be more flexible and more important is that it allows programmers to more clearly understand the form of the construction process, so as to enhance understanding, why not?

Since the classic "Windows programming" has not yet started, I found a good tutorial on the internet, called "Theforger's Win32 API": http://winprog.org/tutorial/

And today, due to some delays, it has only progressed to its two hours of content:

First, the author releases a simple applet:

#include <windows.h>

int WINAPI WinMain (hinstance hinstance, HInstance hprevinstance,

LPSTR lpcmdline, int ncmdshow) {

MessageBox (NULL, "Goodbye, cruel world!", "Note", MB_OK);

return 0;

}

The author certainly reminds me to name this code in *.C, but we certainly don't stop at the point where he says how we do it, but unfortunately the code saved with *.cpp is not compiled, so it's done as the author says, and then successfully compiled and executed.

The result is (still want to vomit trough the author so pessimistic mood, should not be "Hello world!" ?! The effect is not the same as I do not nervous, because WIN10 is such a title bar, at first I also very tangled.

Now that we're done, let's analyze the code for this little applet:

First of all, the #include<windows.h>, this header file I am not familiar with, and now the network is not convenient, but also in MSDN did not find, and so on when the network convenient time to carefully understand the role of it.

The next step is the WinMain () function, which is said on MSDN, " just as every C application and C + + application takes the main function as a starting point, each WIN32-based application must also have WinMain functions." "Then let's take a closer look at it:

The user-provided entry point for a graphical windows-based application.

WinMain is the conventional name used for the application entry point.

With these two words we learned that it is the same as the main () function in C, but its parameters are really worth pondering

int CALLBACK WinMain (

_in_ hinstance HInstance,

_in_ hinstance hPrevInstance,

_in_ LPSTR lpCmdLine,

_in_ int nCmdShow

);

Some of its arguments are interpreted like this:

HINSTANCE [in]

Type:hinstance

A handle to the current instance of the application.

An handle of the current instance of the application, which is usually interpreted as a "handle", although I do not know why this translates = =!

hPrevInstance [in]

Type:hinstance

A handle to the previous instance of the application. This parameter are always NULL.

A handle to an instance on the program, which is usually null

lpCmdLine [in]

Type:lpstr

The command line is the application, excluding the program name.

To retrieve the entire command line, use the GetCommandLine function.

The command-line argument of the program, which is a string, but does not contain the program name

nCmdShow [in]

Type:int

Controls How the window was to be shown.

How to display the window when you control it

This is the list of type values that MSDN gives, which the author says is an integer to pass to ShowWindow (), and we'll look at this function later.

http://msdn.microsoft.com/EN-US/library/ms633559 (V=vs.85,d=hv.2). aspx

OK, so let's move on to this function body:

MessageBox (NULL, "Goodbye, cruel world!", "Note", MB_OK);

Again a function of the call, as the name implies this is a small window of information hint (because it is a box,= =!) The author may think that this function is too simple to explain in detail, so let's take a look at it briefly.

Displays a modal dialog box that contains a system icon, a set of buttons, and a brief application-specific message, such As status or error information.

The message box returns an integer value that indicates which button the user clicked.

Displays a dialog box that contains the system icons, some buttons, and a brief modal message about the program, such as program status or error messages. (for modal and non-modal, which should be mentioned later) this message window returns an integer indicating which button the user clicked.

int WINAPI MessageBox (

_in_opt_ HWND hwnd,

_in_opt_ LPCTSTR Lptext,

_in_opt_ LPCTSTR Lpcaption,

_in_ UINT Utype

);

Let's take a look at the parameters of this function:

HWnd [in, optional]

Type:hwnd

A handle to the owner window of the message box to be created.

If This parameter was NULL, the message box has no owner window.

A handle indicates the window that created the message prompt, and if this parameter is NULL, it does not have a parent window

Lptext [in, optional]

Type:lpctstr

The message to be displayed.

The message that will be displayed

lpcaption [in, optional]

Type:lpctstr

The dialog box title.

If This parameter are NULL, the default title is Error.

The caption of this dialog box, if this parameter is NULL, the default caption is error

Utype [in]

Type:uint

The contents and behavior of the dialog box.

This parameter can is a combination of flags from the following groups of flags.

The content and performance of this dialog (note that what is referred to here is not the information it displays, the information displayed is indicated by the second parameter), and this parameter can be used together with the following tags:

To indicate the buttons displayed in the message box, specify one of the following values. (Button)

To display a icon in the message box, specify one of the following values. (Icon)

To indicate the default button, specify one of the following values. (indicates default buttons)

To indicate the modality of the dialog box, specify one of the following values. (Dialog form)

To specify miscellaneous options, use one or more of the following values. (others)

return value:

Return value

Type:int

If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Canc El button is selected.

If The message box has a no Cancel button, pressing ESC has no effect.

If The function fails, the return value is zero. To get extended error information, call GetLastError.

If The function succeeds, the return value is one of the following Menu-item values.

This is the description of the complete MessageBox (), and if you want more intuitive and complete information, check out MSDN directly.

http://msdn.microsoft.com/EN-US/library/ms645505 (V=vs.85,d=hv.2). aspx

Time is not too early, today first to here, tomorrow continue to record, this is the first lesson of the content!

WIN32 Api_ the first class, Simple WIN32 program creation

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.