MFC subclassification technology

zhaozj2021-02-16  73

Windows is a message-based system, and the message is delivered between the Windows objects. Subcort and Windows hook mechanisms are present in the message system, we can use these mechanisms to manipulate, modify even those who are delivered in the operating system or in processes to change the behavior of the system. Subclassics are used to intercept messages between windows or controls, of course, is the operation that is completed before the window arrived. These intercepted messages can also be reserved or otherwise modified, followed by continuing to send to the destination. Subclassics have realized some of the normal conditions that cannot be implemented. Imagine the right mouse button to pop up UNDO, CUT, COPY, PASTE and other menus, we can use subclassics to change this system menu.

Simply put, subclass is to create a new window message processing process and insert it before the original default window message processing process. Subclass is divided into three categories: instance subclassing - intercept messages from windows or controls, this kind of subclassic technology is most common; global subclassing - can intercepting the same window A number of windows or controls created by the class; superclassing - and global subcatenation, the difference is that it can be applied to the new window class. First, let's take a look at this C program: #include

Using namespace std;

Class Parent

{

PUBLIC:

Void func {cout << "parent" << endl;}

}

Class Child: Public Parent: Public Parent

{

PUBLIC:

Void func {cout << "child" << endl;

}

void main ()

{

PARENT P;

CHILD C;

p.func ();

C.func ();

}

Now I will explain it. In this code I defined two C classes: parent class and subclass, and subclasses are inherited from parent class; they have a member function of the same name. In the main function, I constructed the object of the parent class and subclass, and called their respective member functions func. The results are as follows:

Parent

Child

Simply put, this code is that the subclasses have rewritten the FUNC member function according to their needs. The principle of subclass of Win32 is similar to this, but does not actually serve which functions like C actually, but rely on some of the messages in the Windows system from others to handle it. For example, please see the following simple window callback processes:

Lresult Callback Procmain (HWND HDLG, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)

{

Switch (msg)

{

Case WM_Close:

EndDialog (HDLG, 0);

Break;

Case WM_DESTROY:

PostquitMessage (0);

Break;

}

Return 0;

}

In this callback, I manually handled two messages: When I click the "Close" button (WM_Close), I will close the dialog box; when I destroy (wm_destroy), I am to the system when the dialog is destroyed (WM_DESTROY) The message queue sent an exit message to complete the endquitmessage. That is, if the response code of WM_Close is changed: Case WM_Close:

ShowWindow (HDLG, SW_MINIMIZE);

Break;

In this way, this dialog will be the same as MSN, and after clicking "Off", it will complete the minimization. So, how do you manually respond to its messages for the window procedure?

We can use a function pointer to intercept the message we are interested in and processes the predefined window process after processing. This process is approximately as follows:

WndProc Oldproc;

OldProc = (WndProc) setwindowslong (hwnd, gwl_wndproc, (long) newproc

Of course, the new window process here NewProc is pre-implemented by you. After the above code is executed, the system will first enter the newProc callback process you implemented, and then after processing the message you interested, go back to the original. Complete the remaining work during the callback process.

The above is the principle analysis of the window subclass. Here I actually explain how to subclassify the window through an instance. When we need a special Edit to limit the input of floating point, existing Edit can't complete this work - because it can only limit case sensitive or pure numbers. "Subcorties" can be used.

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

New Post(0)