WINDOWS 9XNT environment C programming quick start (elderly written from the old hard drive, missing)

xiaoxiao2021-03-06  66

C-program design in Windows 9x / NT Environment Quick Start

First look at a very simple program

/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- -

// Winfirst.c

#include

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

MessageBox (Null, Text ("Hello, Windows 98!"), Text ("Hellomsg"), 0);

Return 0;

}

/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- ----

First we see #include , this is the header file that must be included in the Win32 program written with C, which has declated most of WINAPI. (For example, the following MessageBox function).

Note: API = Application Program Interface, application software programming interface, system interruption in DOS age is API

We all know that there is a main function under DOS as the main part of the program. In the Win32 environment, use WinMain instead. (WinAPI represents a call habit for the clearance of the stack space for constraining functions. Now there are two popular call habits, __ cdecl and __stcall, __ cdecl is the default call habit of C / C language, the stack is cleared by the caller, support Variable parameters .__ stdcall is the call habit of Pascal, the stack is cleared by the function, does not support the change function .WinaPi = __stdcall, all Win32 functions use __stdcall call habits.) This function has 4 parameters, where hprevinstance is 16 Located in Windows, now there is no use, SZCMDLINE is the command line parameter.

Hinstance is called an "instance handle". This parameter is passed by the operating system

ICMDSHOW represents the way the window is displayed, this parameter can control the appearance of the application at startup.

How to compile the program?

This is a problem that many students will encounter (behind the backward textbooks, backward tools (BC 3.1)). I recommend using VC6 to compile this program. Many students feel that VC6 is very difficult. In fact, he is just a tool. It can be used to cut the tissue. As long as you master a little bit, you can bring a lot of convenience for future learning.

Click | File | New |, select "Win32 Application" to create in the "Project" tab. Select "A Simple Win32 Application" in the next dialog, WIZZARD generates code as follows

#include "stdafx.h"

Int apientry Winmain (Hinstance Hinstance,

Hinstance Hprevinstance,

LPSTR LPCMDLINE,

INT ncmdshow)

{

// Todo: Place Code Here.

Return 0;

}

Just add code to "Todo: Place Code Here". As for #include "stdafx.h", don't take him, where there are #include "windows.h". Here we first replace "// Todo: Place Code Here"; then press F7 to compile, Ctrl F5 run. Or press F5 to debug operation.

What is Windows?

It looks very much from the appearance of Windows, with toolbars, menus, scroll bars, dialogs, etc. There are also many various UI (user interface) elements. But keep in mind that all of this is Window, that is, the surface of Windows is composed of Window. Remember such a word "Windows is a zone", those colorful elements are only different from their respective Window properties. In the future, I wanted to have problems from this perspective, and many interface issues can quickly find the idea. (I understand this for 10 months of time ... As for the core part of Windows, it is composed of process (Process) that must be in each operating system.

Windows is object-oriented

When I talk about object-oriented, many people will boil, cheers. Yes, oo (object-oriented) is really great, but I don't know how many students have really understood the meaning of OO in school. Under the current education influence, many students talk about OO, think of a class, think of inheritance, etc. Language characteristics. In fact, OO is language-independent. For the current situation, OO's concept has changed every day. When you can use OO to use OO, you can use OO to describe the problem, OO's concept will not be a true OO. (Say far, this topic will be discussed next time. This key is to say that Windows and OO relationship, huh, huh).

Why is the Windows written by C is OO, because in the world of Windows, it is basically strictly strictly complying with the rules of the OO world, and is the cross-language application of OO concept. Here is a few of what I think is the most critical.

Object)

In the world of OO, all things are objects. One of these most important concepts is "Object Identity). In the world of OO, each object has a unique identifier, which can be used as a different software entity identifier, processing characteristics. Usually this identifier is called a handle, each of the true OO world handles meets 2 conditions. The first is "In any case, the object maintains the same handle throughout the lifecycle", another "two objects that cannot have the same handle". These two principles are quite important. In C , the object handle is a pointer, and two different objects can be distinguished by pointers.

The most intuitive object in the Windows environment is a one of Window. These Windows have a handle syndrome (Handle), which has a Window handle, is equal to having this Window object.

Class (Class)

In the world of OO, every object must belong to a certain class. Class can be said to create an object template, each object created from the same class has the same structure and behavior. (There is a theory that every class in the OO world must be inherited from an identical base class, but I don't think it is.)

There are multiple classes in a Windows environment, and these classes are generated by different areas. (Note! The objects created by different classes have a completely different, unique handle in the same field. For example, GDI interface objects and Windows core objects (regarding the differences between specific GDI objects and Windows core objects, later) Windows identifies all objects through 32-bit Handle. The parameters of the Winmain function above represent a "instance object".

Message

In the world of OO, there must be messages between objects and objects to communicate, and can receive messages are the basic capabilities of objects. The message can be understood as a "carrier" that the object 1 is requested to operate. The message has 4 elements: the sender of the message, the recipient of the message, the name of the request operation, and the information required to operate. Object A To send a message to the object B, you must first know that the object ID of the object B is. A simple example, a statement in C

Obja.data = objb.data;

The message translated into oo is: Object OBJA sends a message to the OBJB, the message is operated as "get_data", and the additional message is not.

Now we already know that the most basic object in Windows is a Window that requires these WINDOW to make a message to send them a message. Windows performance is very abstract at this point. It is easier to understand that sending messages to a Window object by calling the SendMessage this API function.

For example, a statement

SendMessage (Hobj, Message_Move, Info, Callbackfoo);

If you use a structured vision, just call a function, but use OO's eyes, this statement should be interpreted as "send a message to the object OBJ identified by the HWnd, request the OBJ to perform the operation Move, this operation needs to be added Information INFO, Callbackfoo. The language describing the language is: "Hobj.move (Info, Callbackfoo)" When you encode yourself, keep clearly understand, I believe that whether it is the understanding of OO thinking or the understanding of Windows, helpful.

There is also a lot of API's role. It is actually sending a message, but the appearance of their API is easy to cover this, such as

INT getWindowText

HWND HWND, // Handle to Window or Control

LPTSTR LPSTRING, // Text Buffer

INT NMAXCOUNT / / MAXIMUM NUMBER of Characters To Copy

);

This API uses OO's vision to call this API to send a message to the WINDOW object identified by the HWnd, requests "getText", and additional information is lpstring and nmaxcount.

The first orthodox Windows program

The source code is as follows:

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------

HelloWorld.c

Copyright © Waterflier

-------------------------------------------------- ---------------------------- * /

#include

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, PSTR Szcmdline, ICMDSHOW)

{

Static tchar szappname [] = text ("hellowin");

Hwnd hwnd;

MSG msg;

WNDCLASS WNDCLASS;

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WNDPROC;

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = HINSTANCE;

WNDCLASS.HICON = LOADICON (NULL, IDI_Application);

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow);

WNDCLASS.HBRBACKGROUND = (Hbrush) getStockObject (White_brush);

WNDCLASS.LPSZMENUNUNAME = NULL;

Wndclass.lpszclassName = szappname;

IF (! registerclass (& wndclass))

{

Messagebox (Null, Text ("Cann't RegisterClass!"),

Szappname, MB_ICONEROR);

Return 0;

}

HWND = CREATEWINDOW (Szappname, // Form Class Name

Text ("THE Hello Program"), // WINDOW title

WS_OVERLAPPEDWINDOW, // WINDOW's appearance type

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

Null, // Parent Window Handle

Null, // Window Menu Handle

Hinstance, // program instant sequence

NULL);

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

Return msg.wparam;

}

Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM)

{

HDC HDC;

Paintstruct PS;

RECT;

Switch (Message)

{

Case WM_CREATE:

// Plays (Text ("Hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);

Return 0;

Case WM_Paint:

HDC = BeginPaint (HWND, & PS);

GetClientRect (hwnd, & review);

DrawText (HDC, Text ("Hello, World!"), -1, & review, dt_singlex | DT_Center | DT_VCENTER);

Endpaint (hwnd, & ps);

Return 0;

Case WM_DESTROY:

PostquitMessage (0);

Return 0;

}

Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);

}

Running rules for Windows programs

After the traditional DOS program is loaded by DOS, the operating system is no longer inserting the program. The Windows program is completely different. We know that if you want to operate an object in the world, the means used is to send messages, and the object that receives the message must have the ability to process the operation requested by the message, so the Windows program is "message driver "of. The main process of the Windows program design is to write a message code that sends a request operation and an object processing operation of the received message.

After loading by the operating system, the Windows program is first running the Winmain function, the main purpose of this function is to create an object (of course, you can create an object in other places), which generally creates a Window object. Then WinMain enters a message loop

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

The user's operation (mainly from hardware) is converted to a message via the operating system, the operating system places this message into a message queue of an object (the operating system must know which object to be put, the specific mechanism will be detailed later) The application captures a message from the message queue from the message queue by GetMessage (& MSG, NULL, 0, 0), which can be converted to this message (TRANSTEMESSAGE (& MSG)); Specific objects. (Since the DispatchMessage API is an operating system implementation, the control of the program is also returned to the operating system. The operating system will send this message to a specific object.) So the Windows program requires the intervention of operating system at runtime To maintain the various rules of the OO world.

Generate the first Window object

Creating an object first needs to know the class belonging to this object. Although all Window objects, these objects have different individuals, so they need to be created from different WINDOW classes.

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WNDPROC;

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = HINSTANCE;

WNDCLASS.HICON = LOADICON (NULL, IDI_Application);

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow);

WNDCLASS.HBRBACKGROUND = (Hbrush) getStockObject (White_brush);

WNDCLASS.LPSZMENUNUNAME = NULL;

Wndclass.lpszclassName = szappname;

IF (! registerclass (& wndclass)) {

Messagebox (Null, Text ("Cann't RegisterClass!"),

Szappname, MB_ICONEROR);

Return 0;

}

This paragraph is to define a Window class and register this class to the system's Runtime Type Library (Type Library is also an integral part of the OO world, which is better understood). All fields of WNDCLASS are very interesting, it is also important, these fields define many of the most basic properties of the objects created from this class. The most important thing is the lpfnwndproc field, the type is a callback function pointer. This function defines the ability of the Window class to process "requests". Another important field is STYLE, defining the object generated by the class to receive those messages. Another important field is lpszclassname, defined the name of the class, and later create a class of objects, this value is important. (I personally feel that using a string as a class identity in the OO world in many classes, why not use guids). Other Fields classmates can check MSDN themselves.

转载请注明原文地址:https://www.9cbs.com/read-89782.html

New Post(0)