Why should we use class function instead of API in VCL, why use CM_ message instead of virtual function

Source: Internet
Author: User

The reason to use the class function instead of the API is that the VCL does some packaging for it, but before and after the API works, it does something extra: notification and judgment, and so on.
The reason the class function wraps a CM_ message is that it makes it easier for programmers to truncate and rewrite this process

Example:

proceduretwincontrol.invalidate;begin//Note that it is a simple execution function that treats the message as a parameter, not a message Perform (Cm_invalidate,0,0); //Note that the second parameter, wparam, is 0, which requires the API to invalidate itself, rather than just a notification function. End;procedureTwincontrol.cminvalidate (varmessage:tmessage);varI:integer;begin//This function is inherited by all Windows child controls, as long as the programmer (or control programmer) is not overwritten, then the entire client area will be declared as invalid area using the API, in fact, the process is very simple//The only thing to note is that, before the child control declares an invalid zone, all ancestor controls are notified, up to Form1, and Form1 no longer have the parent (but there are parents)//in fact, the end is simply call API, but is the VCL library framework wrapped over//This is the premise, anyway, the win control should have a handle. If you don't have a handle yet, you don't have to notify, not to call the APIifhandleallocated Then  begin//The equivalent of a child control to redraw, you must notify the parent control, and the notification, the parent control to deal with the processing, not processing even if (in most cases will not be processed). ifParent <>Nil  Then//perform will go to the control's own virtual function WndProc to find the message processing, rather than directly to the message index function processing. Fixme do so much work, will not be inefficient ah//Note that the second parameter, wparam, equals 1. Parent controls They just know that they don't really repaint themselves (unless they overwrite the cm_invalidate message).      The second parameter to compare the invalidate function is 0, which is the true redraw. //fixme question: When will you use this trick?      Answer: The programmer will use this function when it is overwritten. //the handling of the win control is usually enough to handle itself.      But there is an additional interface, and the Stdctrls unit and the forms unit do not handle this function, but the Tsalphalistbox unit is used more. Parent.perform (Cm_invalidate,1,0); //recursive, notifies the parent class first. //when the parent control is finished notifying you, it's time to repaint yourself. //Note that this wparam parameter is equal to 0, only in this case, the description is invalidate here straight over, will redraw themselves. ifMessage.wparam =0  Then    begin//just identify the invalid area, not really redraw, redraw and so on after receiving the WM_PAINT message by calling the Painthandler function, identify the invalid area at any time can//wait until the system is free to detect invalid areas automatically, if any, send you WM_PAINT message//Csopaque is not in the style of the background redraw. Csopaque in style The background is not redrawn (opaque, the position occupied by the current control in the parent control is obscured, so the background color does not need to be redrawn). (Only the client area of the current control needs a parent control to help draw) (only when you are transparent)//Fixme background should not refer to your client area, but the client area of its parent control? InvalidateRect (Fhandle,Nil, not(CsopaqueinchControlStyle)); //API, the second parameter is NULL, the entire client area is redrawn, and the third parameter, true, redraws the background. {Invalidate child windows which with the Parentbackground when themed}//Fixme only the child controls are redrawn when the theme is applied.      After testing, classic and blue sky clouds are not applied theme? ifThemeservices.themesenabled Then         forI: =0  toControlcount-1  Do//once the parent win control background is updated, it is necessary to notify the graphics child control, so long as it needs the background of the parent control, it will have to redrawifCsparentbackgroundinchControls[i]. ControlStyle Then//important controls[i].    Invalidate; End; End;End;

Why should we use class function instead of API in VCL, why use CM_ message instead of virtual function

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.