Contact VC

zhaozj2021-02-16  51

Preset directory

:

Part 1: Winsdk

Part II: MFC Class Foundation, C Program Writing Specification Introduction

Part III: MFC Based on Dialog Box Program

Part IV: DLL Dynamic Link Library

Part 5: COM component foundation

Section 6: Several common technology of COM components: iDispatch, connectable objects.

Part 7: Application of COM components, and MFC COM writing

Part 1: Winsdk

Learning VC has been in recent years, and I review my learning history is quite hard. There is no teacher to teach, there is no good book in the library. In order to buy a good book, the silver on the body is a few flowers, and there is no life to have a harvest. I have been working on what I write in recent days, I can summarize my learning situation. Second, please teach the master, you can help point out the wrong leak. Three, maybe it will help beginners. Most, you can practice writing and prepare for college entrance examination.

OK. Let's talk about it. First, the MFC (Microsoft Foundation Class) is a class library for building a Win32 program, but the fundamental words are based on Windows old ancestors - Windows SDK. Its history can be traced back to the Window 3.x era. Its entire framework is written by C. More importantly, its program structure is still the foundation of the Win32 program. Since it is too cumbersome, it is not necessary to write programs in general. Therefore, in the process of learning VC, understanding its program structure is the root of Win32 programs, but there is no need to learn Windows SDK. ^ _ ^, This is also a tribe for me to use Winsdk so far.

Below, it is an example of a WinsDK program (taken from Hou's "deep-in-depth MFC", there is a traditional electronic version of this book at Houjie Station)

INT Callback Winmain (Hinstance Hinstance, Hinstance Hinstance, LPSTR LPCMDLINE, INT NCMDSHOW)

{

MSG msg;

IF (! hprevinstance)

IF (! initApplication (hinstance))

Return (false)

IF (! initinstance (hinstance, ncmdshow))

Return (False);

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

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

Return (msg.wparam);

}

BOOL INITAPPLICATION (HINSTANCE HINSTANES)

{

Wc.style = CS_HREDRAW | CS_VREDRAW;

Wc.lpfnWndProc = (WndProc) WndProc;

wc.cbclsextra = 0;

wc.cbWndextra = 0;

wc.hinstance = hinstance;

Wc.hicon = Loadicon (Hinstance, "JJHOURICON");

Wc.hcursor = loadingCursor (NULL, IDC_ARROW);

wc.hbrbackground = getStockObject (White_brush);

Wc.lpsz GeneNuname = "genericmenu";

wc.lpszclassname = "myWindow";

Return (RegisterClass (& WC));

}

Bool InitInstance (Hinstance Hinstance, Int ncmdshow)

{

HWnd = CREATEWINDOW ("MyWindow",

"This is Windows SDK", WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

Hinstance,

NULL);

IF (hwnd == null)

Return (False);

ShowWindow (HWND, NCMDSHOW);

UpdateWindow (HWND);

Return (TRUE);

}

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

{

Switch (Message)

{

Case WM_DESTROY:

PostquitMessage (0);

Break;

DEFAULT:

Return DefWindowPro (HWND, MESSAGE, WPARAM, LPARAM);

}

Return 0;

}

First, the entry function in the C language is the main function, which is WinMain in the Win32 program. Its prototype is

Int WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT CMDSHOW)

The first parameter is Hinstance, which is the current programs for this program. What is the example? When you run twice with the same Windows program, there will be two exactly the same window on the screen; then we said that this program is generated by two instances. Again, in your eyes, the apple is the case of Apple's three instances of Apple. According to this idea, the instance is an entity in an abstract concept. So strictly, what we have written is not this program, but the logic of this program. The program instance is the entity result generated by this logic we have written. The handle is used to represent the instance, it is a 32-bit value. Each instance is a 32-bit value, and this value is called a handle. I don't know what I said, I don't know. The Instance here is the handle after the current program is instantiated. And the resources belonging to the same example are shared. The second parameter HPREINSTANCE represents the previous (previous) program instant handle, this value is always empty (zero) in the Win32 program, the third parameter is lpcmdline, this is a string, when the launch is recorded The command line, the role of the main function (Argc, Argv) in C. Finally NcmdShow indicates whether the window of this program is displayed.

In the main function, the first function to be executed is initApplication (), the statement is as follows:

IF (! initApplication (hinstance))

Return (False);

The role of this function is to register the window class. The registration window is to tell the system to link our icons, menus, message processing functions, cursors, and cursors to become a window class, prepare for the back display window. Use registerclass () to register the window in the initApplication () function. Returns true if success, otherwise returns false. Its unique parameter is a WNDCLASS structure. The functions of each of the fields are as follows: (Please see the MSDN regarding the specific value)

STYLE specifies window style, such as the CS_HREDRAW lifted by the example indicates that the entire window is heard when the horizontal length changes.

LPfnWndProc Specifies the message processing function of the window, the prototype of its function must be

LRESULT CALLBACK ?????? (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM)

^ This name is casual.

Hinstance identifies which program instance of this window belongs to.

HICON identifies the icon of the window icon HcUROSR identifies the cursor handle of the window cursor

HBRBackground identifies the background brush of the window.

Lpszclassname is the name of this window class, which is used for later establishing a window reference window class.

When the registration window class is successful, you can create a window. The main function calls initInstance () to create a window. Use createWindow () in the InitInstance () function, to create a window, where the first parameter is the name of the window class, the content of the LPSZClassName field when the window class is registered. If success, return the handle, and the handle represents a good window (instance). Otherwise, the handle returned is empty. When the establishment of a good window is not displayed immediately, you also need to use ShowWindow to make the system display window, which is a window handle (HWnd). Look, in WinsDK is a handle to identify anything (program, window, icon, cursor, file, etc.). Then use the updateWindow () to let the window redraw over the screen so that it is really displayed. Finally returned True.

Then there is an important section of the WinsDK, a message loop:

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

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

In each program instance, a message queue is configured. When the message is generated (such as: refreshing the wm_paint, the mouse moves WM_MOSEMVOE, the keyboard is pressed to WM_KEYDOWN, etc.), it will put it into the tail of the queue. The program uses a while cycle, which is a famous message loop. GetMessage () Removes a message in the queue header in the first parameter MSG, if it is not a WM_QUIT message, returns true if it is WM_QUIT to return false, and end the loop. The first function in the loop translateMessage () is the keyboard conversion function that converts the virtual key message into a character message:

* WM_KEYDOWN / WM_KEYUP combines a WM_CHAR or WM_DEADCHAR message

* WM_SYSKEYDOWN / WM_SYSKEYUP is combined into a WM_SYSCHAR or WM_SYSDEADCHAR message.

And put the combined messages in the queue, wait for later processing.

The DispatchMessage () function is to transfer the message to the corresponding window message processing function. In the MSG structure, there is an HWND field that records the window that generates a message. DispatchMessage will find which window class belonging to this HWND handle, and incorporated the message to the message processing function in this window class (pointing to the lpfnwndproc field in the WNDCLASS structure) Function) goes. This completes its message allocation. In the sample program, only one window class is registered. So all messages are generated by this window instance, so all messages will be incompatible in WinProc.

In the entry parameters of the message processing function, it is the main information identified by a message, the source window of the HWnd, and the type of Message message indicates what message has occurred, WPARAM, LPARAM and message bundle. Since the type in C / C is free to convert, it is inconsistent according to different messages. Different messages are generally used in message processing functions. I believe that everyone will understand what happened.

Regarding the information in Message has a detailed definition in the VC98 / Include / WinUser.h (reference line 1292), the meaning of each message can be checked in the MSDN.

The above is what I know. Winsdk everything. If there is any mistakes and omissions, please write to me: Blue_atlantis400@hotmail.com.

The first part is finished.

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

New Post(0)