Differences between super class and sub-class in C ++

Source: Internet
Author: User
Tags inheritance

Super class and sub-class in C ++

There is no specific code for superclass and subclasses. It is actually a programming technique. There can be different implementation methods in MFC and WTL.

Window subclass:

The principle is to change the window procedure function of a created window class. By intercepting messages of the created window, you can monitor or modify the behavior attributes of the created window class. It can be used to change or expand an existing window without re-development. For example, to obtain the functions of the predefined control window class (button control, edit control, list control, drop-down list control, static control, and scroll bar control), you must modify some of their behaviors.

The advantages of subclass are mainly reflected in the following two aspects: first, it does not need to create a new window class, and does not need to understand the window process of a window. This is useful when the original window functions are compiled by others and the creation process is invisible. Secondly, subclass is easier to implement, because all you need to do is write a window function.

The main steps are as follows:

Intercept the message to prevent it from sending it to the original window function.
Modify the message.
After the modification is completed, send it to the original window function.

// Save the window's default message response function pointer WNDPROC pSubclassOldEditProc; // replace the message response function lresult callback JcEditProcSubClass (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_CHAR: {: MessageBox (hWnd, "WM_CHAR response", "subclass", MB_ OK); return 0 ;}// after use, the message is sent back to the original form default: return: CallWindowProc (pSubclassOldEditProc, hWnd, message, wParam, lParam );}} // SubClass code for the created form {// create HWND hEdit = createjavaswex (NULL, "EDIT", "SubClass", WS_CHILD | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL, 100,120,128, 16, hWnd, NULL, hInstance, NULL); // modify the window attributes and change the message response function pSubclassOldEditProc = (WNDPROC): SetWindowLong (hEdit, GWL_WNDPROC, (DWORD) JcEditProcSubClass ); // Display ShowWindow (hEdit, nCmdShow); UpdateWindow (hWnd );}


Window Superclass:

Window superclass change the features of window classes at the WNDCLASS or WNDCLASSEX level. Changes the behavior attributes of existing window classes.

Call GetClassInfoEx to obtain information about the window class to perform the superclassing operation. The GetClassInfoEx function requires a pointer to the WNDCLASSEX structure, which is used to fill in the window class information when the return is successful.
Modify the WNDCLASSEX structure members as needed. Two members must be modified:
HInstance instance handle for storing programs
LpszClassName: pointer to a new class name
You do not need to modify the member lpfnWndProc, but in most cases it is still needed. But remember that if you want to use the CallWindowProc function to call the old window, you must save the original value of the member lpfnWndProc.
Register the modified WNDCLASSEX structure to obtain a new window class with some features of the old window class.
Create a window with the new window class.

WNDPROC pSuperOldEditProc; // Default message processing function of the Save window // The CALLBACK function lresult callback JcEditProcSuper (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_CHAR: {: MessageBox (hWnd, "WM_CHAR response", "Superclass", MB_ OK); return 0;} default: return :: callWindowProc (pSuperOldEditProc, hWnd, message, wParam, lParam) ;}// create the code of the superclass control {// obtain the original control information WNDCLASSEX myeditClass;: GetClassInfoEx (hInstance, "EDIT", & myeditClass); // Save the default message processing function pSuperOldEditProc = myeditClass of the original control. lpfnWndProc; // sets the replaced message processing function myeditClass. lpfnWndProc = JcEditProcSuper; // specify the new window class name myeditClass. lpszClassName = "JcilyEdit"; // you can specify the size of the struct myeditClass. cbSize = sizeof (WNDCLASSEX); // register the new message RegisterClassEx (& myeditClass); // create HWND hEdit = createmediawex (NULL, myeditClass. lpszClassName, "SuperClass", WS_CHILD | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL, 100,100,128, 16, hWnd, NULL, hInstance, NULL); // Display ShowWindow (hEdit, nCmdShow ); updateWindow (hWnd );}


Differences between window subclass and superclass

(1) modify window process functions by subclass, and modify window classes by superclass (new window class name)
(2) subclass is at the window instance level, and superclass are at the WNDCLASS level.
(3) superclassization can accomplish more complex functions than Subclass. In the SDK category, subclass can be considered as a subset of superclassization.
(4) subclass can only change the nature of the window created after it is created. There is nothing to do with the window creation process (ON_CREATE events cannot be intercepted), while superclass can be implemented. Superclass cannot be used in Windows, subclass.
(5) attributes such as the background of a form can be modified for superclass, but not for subclasses.


Differences between class C ++ and subclass

Q: I have read the WINX Development Kit Documentation. What is the concept of a superclass? I have heard of Subclass only ).

A: Subclass refers to the process of replacing windows (WNDPROC ).
Superclass is the process of replacing the window (WNDPROC) and replacing the ClassName.
Subclass is not like inheritance, but like a plug-in (Hook) action.
Superclass is more like inheritance, because a new window class is generated and the behavior is inherited.
In winx, Subclass and Superclass are implemented using the same class. All are winx: SubclassWindow.
Other interface libraries generally do not provide Superclass.
 
Q: Do you say other interface libraries do not provide Superclass? Should the inheritance in MFC belong to Superclass?

A: No. Subclass is used in MFC.

Q: Why can't I see the difference between the two? I mean in terms of usage and results.

A: The two are different in usage, but the obtained results are indeed not much different.
Take the Button as an example. If Subclass is used, the user must first have a Button and then Subclass.
That is to say, Subclass occurs after CreateWindow.
If it is a Superclass, you can directly input a new window class name during CreateWindow. No Button is generated.
Of course, this requires that the RegisterClass of the window class be called before CreateWindow.
MFC uses Subclass. Because the CButton class is associated with the Button on the dialog box through DDX technology.
That is to say, in MFC, a Button is first available, and then it is Subclass by DDX technology.
Therefore, you generally cannot see the Subclass process.
 
Q: Under what circumstances do superclasses need to be used?
Oh, I understand, is there a SuperClass technology that allows users to easily create their own Control?
 
A: Yes. It does not need to start from scratch.

Q: Yes. Most custom controls are related to system controls.

A: Yes. This is exactly the meaning of the existence of Superclass.
You can imagine: In the past, you provided a control, you want to tell it, first create a Button, and then call my Subclass function.
With Superclass, you only need to tell it the name of the window class.
Because Superclass hides the fact that you inherit from the Button.
 
Q: Yes.

A: The reason why winx can have superclass and other interface libraries is not, it is related to the starting point of shielding the "window class.

Q: Yes, yes, yes. You can also visually develop custom controls in the future.

A: Yes.

Q: Just specify our own window class.
WINX is a control between system controls and ActiveX controls.

A: Yes. This control is actually the implementation of Windows system controls.
However, you do not need to register the system control. Windows has already completed registration for you.
 
Q: Didn't the system implement these system controls through SuperClass? It should be inherited from the most general window.

A: Oh, of course not. They have nothing to borrow, so they have to inherit from the most basic winx: Window.

Q: whether it's a startup or a little basic, it's a SuperClass.
Because winx: Windows is based on DefWindowProc and is also a Window process.
 
A: This is understandable.

Q: Is it understandable that SubClass only changes WndProc, while SuperClass also changes other window properties?

A: Yes. Your understanding is completely correct. This is the essential difference between Subclass and Superclass.
Superclass can change any data of window class (WNDCLASSEX. This is how WINX is implemented.

Subclass sample code:

// Users // class CMyEdit-use the Subclass technology class CMyEdit: public winx: Edit <CMyEdit> {public: VOID OnContextMenu (HWND hWnd, winx :: CPoint pt) {// The right-click menu is disabled ...}}; // Configure // CHelloDlgclass CHelloDlg: public winx: ModalDialog <CHelloDlg, IDD_HELLO> {public: BOOL OnInitDialog (HWND hDlg, HWND hWndDefaultFocus) {CMyEdit: offline (hDlg, IDC_EDIT1); return TRUE ;}}; // optional int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {CHelloDlg dlg; dlg. doModal (); return 0 ;}//-------------------------------------------------------------------------



Code of the superclass:

// Category // class CMyEdit2-use Superclass technology class CMyEdit2: public winx: Edit <CMyEdit2> {WINX_CLASS ("MyEdit"); public: VOID OnContextMenu (HWND hWnd, winx: CPoint pt) {// The right-click menu is disabled ...}}; // Configure // CHelloDlgclass CHelloDlg: public winx: ModalDialog <CHelloDlg, IDD_HELLO>; // specify int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {CMyEdit2: RegisterClass (); CHelloDlg dlg; dlg. doModal (); return 0 ;}


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.