A deep understanding of the similarities and differences between Tcontrol and Twincontrol

Source: Internet
Author: User

Tcontrol is a graphical control, it does not have a handle, so you can not directly use the WINAPI display, adjust location, send messages and so on, can only find a way to indirectly achieve the desired effect, but may directly use some do not need to handle the API, such as InvalidateRect.
Twincontrol is a window with a Windows handle, and it has a handle, so all WINAPI that use the handle can manipulate it directly to achieve a variety of effects , so that the Windows window can be driven to work properly. So it manages its graphics sub-controls and makes them work normally in their own three-point field ( I understand: in the World of Delphi, a Wincontrol is equivalent to playing the role of windows as a whole, to manage its " child window controls, Tcontrol and Twincontrol, if it's not so strong and important for Wincontrol management, it's very important to manage the Tgraphiccontrol .


for this reason,Twincontrol needs to define a series of class functions, and use them to package the WINAPI to conform to the overall architecture of Delphi's VCL, and to make it better . Tcontrol Although it is not possible to work directly with the WINAPI driver , the VCL authors rescue use its parent control handle and use the same function name to achieve the same effect (That's why Levi mentions a defect in a VCL-- Tcontrol.parent must be a twincontrol, this is because he did not realize that Borland is deliberately forced to do so, otherwise it is necessary to judge the parent is Twincontrol, that would not be troublesome to die, it is likely to write many more exception statements. Levi's ideas are not wrong, it is a purely object-oriented theory of thought, but he himself did not deeply participate in the development of VCL, with Borland Masters still some distance, and his own power to explain the structure of the entire VCL and ideas, and actually estimated that Borland long considered this point, How can a man who is so hot and green with Windows programming and OO use such a thing unexpectedly? Therefore, Delphi's approach is often the perfect interpretation of the theory, because it is the pure theory of some flaws in the best make up and balance, and I feel everywhere is this, this is I think Delphi is the most amazing place. The theory of light to realize who will not ah, nothing is to spend a bit of time and cumbersome. As friends say, the world outside C + + is wonderful, do not indulge in the world of C + + for a lifetime, have time to learn a few more languages and to think and compare, I feel that I have benefited from some of Delphi's thoughts and balance scheme. Of course, only indulge in the world of Delphi is not good, my next goal is Golang and C #, haha ha). This is why Tcontrol often have twincontrol functions of the same name, because functions with the same names work better! such as the SetBounds function (which is what I thought when I saw this function):

Procedure Tcontrol.setbounds (Aleft, ATop, Awidth, aheight:integer); begin if Checknewsize (Awidth, aheight) and//Tcontro  Class functions of L ((aleft <> fleft) or (ATop <> ftop) or (awidth <> fwidth) or (aheight <> fheight) Then Begin InvalidateControl (Visible, False);    Tcontrol class function, the second parameter indicates that the current control is temporarily set to be transparent fleft: = Aleft;    Ftop: = ATop;    Fwidth: = Awidth;    Fheight: = Aheight; Updateanchorrules; Tcontrol class functions, coordinates and long-width settings, it is necessary to re-riveting a//property is set, if there is an API can make it work on the spot call (about the Display section, do not need to handle the API use, this is a special case) Invalidate; Tcontrol class function, call Tcontrol.invalidatecontrol, and then call API Declaration Invalid area//This message has corresponding function in Tcontrol and Twincontrol. The graphical control uses the message to do some of its own changes, and the win control uses the message to call the class function to invoke the API to actually function//the former to recalculate the maximum minimized limit and the size of the docking station, which uses the API to adjust the border and the control's own position,    Of course, you have to recalculate the maximum minimized limit and the size of the docking station (sandwich technique) Perform (wm_windowposchanged, 0, 0); The Windows location is adjusted and re-aligned (essentially calling Twincontrol.requestalign and then rearranging the API)//But actually rearranging itself by the parent win control because it has no ability to own other controls,    Of course, you can't actually align all the controls. Requestalign; Tcontrol of the virtual function, each Wincontrol sub-class can be rewritten, such as Tcustomform rewrite    If not (csloading in componentstate) then Resize; Tcontrol a virtual function that simply invokes programmer events.  Subclasses generally do not need to overwrite it. End;end;

We can see that Twincontrol also has the corresponding function, it can use the API directly to achieve the effect, it can even use the Tcontrol definition of some common logic and effects (thanks to OO technology):

Procedure Twincontrol.setbounds (Aleft, ATop, Awidth, Aheight:integer); var windowplacement:twindowplacement; Windows fabric type, containing 6 items, such as maximizing minimized window position, such as Begin if (Aleft <> fleft) or (ATop <> ftop) or (awidth <> fwidth) or (A Height <> fheight) THEN BEGIN//If there is a handle and is not minimized, use the API to immediately adjust the position if handleallocated and not isiconic (fhandle) th En//API SetWindowPos (fhandle, 0, Aleft, ATop, Awidth, aheight, Swp_nozorder + swp_noactivate)//API, use after Windows self Changes the data in the windowplacement else//If there is no handle, or is in a minimized state, use the API to change the value of the windowplacement structure in case the next call to the API directly shows the BEGIN//reset upper left corner sitting Fleft: = Aleft;      Tcontrol class Property, common attribute ftop: = ATop;      Fwidth: = Awidth;      Fheight: = Aheight;        If handleallocated then BEGIN//Get the location information of the window windowplacement.length: = SizeOf (windowplacement); GetWindowPlacement (Fhandle, @WindowPlacement); API//Change window location information windowplacement.rcnormalposition: = Boundsrect; Tcontrol class Property, common property SetWindowplacement (Fhandle, @WindowPlacement);    API end;    End Super before using the API set the real Windows window Properties and Delphi control properties, can be assured to call some of the functions, not Tcontrol has provided the general logic, is that it defines some special functions, can be used casually, directly produce effects,    And no longer rely on others to do something. Updateanchorrules; Tcontrol class function, General function requestalign; Tcontrol class function, General function end;end;

Tcontrol.getdevicecontext This example is also obvious, it uses the parent win control of the same name function to get the DC, and then use this handle call API to get perspective and add a new clipping area, because these two APIs are related to the self-painted API, and Tcontrol is one of the basic design purposes is Displays and trims graphics images, so tcontrol can use them directly to achieve the desired functionality.

function Tcontrol.getdevicecontext (var windowhandle:hwnd): Hdc;begin  If Parent = Nil then    raise EINVALIDOPERATION.CREATEFMT (sparentrequired, [Name]);  Result: = Parent.getdevicecontext (WindowHandle),//Twincontrol class function, not only set the DC, but also set the parameter  handle from the Fhandle of Delphi Setviewportorgex (Result, left, Top, nil);       The API is used to change the viewport port and the origin of the window, and all have the effect of changing the axis, so that (0,0) no longer refers to the upper left corner  IntersectClipRect (Result, 0, 0, Width, Height);//API creates a new clipping region, The area is the intersection of the current clipping region and a specific rectangle end;

A little summary of the Tcontrol alignment process, in fact, it is the same:
Requestalign->aligncontrol->disablealign->aligncontrols (in essence, work alignment)->enablealign->realign-> Aligncontrol (Nil)
The first-level entry function for the entire process is:
But it's just a graphical control that doesn't have the ability to have other controls, and of course it can't actually align all the controls, so do it:
Twincontrol.aligncontrol (Acontrol:tcontrol);
It does the entire alignment logic package, which executes in turn:
Call twincontrol.disablealign; It executes Inc. (FALIGNLEVEL);
Call Twincontrol.aligncontrols (Acontrol:tcontrol; var rect:trect); All the material about control size, alignment, riveting is here
Call twincontrol.enablealign; it calls Twincontrol.realign; It calls Twincontrol.aligncontrol (nil);


Tcontrol has two functions: the first is to define common functions (such as 10 mouse button messages) used by all graphical controls and win controls, and the second is to define common functions that all graphical controls need to use, such as Invalidatecontrol,setcolor, Gettext,settext and so on, countless
Twincontrol two roles: The first is to define the common functionality that the Windows handle control uses (using the API to really work), and the second is to manage the display of its graphical child controls. Many times you have to manage their wincontrol (if Windows doesn't manage them very well).

You can also summarize the functions of Tcontrol and Twincontrol with the same name function, make a list and compare their functions and implementation methods.

A deep understanding of the similarities and differences between Tcontrol and Twincontrol

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.