VCL Source Code Analysis Methodology (taking the origin of the Tbutton.caption attribute as an example)

Source: Internet
Author: User

Recent period seems to be popular source code analysis:) I also talk about in the past a period of time on the analysis method of VCL source code, this article will not discuss the framework of VCL Class library and design pattern of the east, just with our common control properties/methods of implementation of the process to make a simple explanation, hope to help beginners.

VCL Analysis Method
Example: The origin of the Tbutton.caption attribute
(This article is dedicated to Delphi beginners only)
Used for a period of time Delphi friends, will be interested in VCL source code. I also often in the major forums to see some netizens study discussed the source of VCL posts. However, many netizens are trying hard to understand, but finally still halfway, because they always can not get a clue, see Foggy. I also have the habit of looking at the source code, when it is OK to point the right mouse button, always hope to get some lucky harvest and development skills. But everything has to have a basic pre-problem, just like the process of people going to school (here refers to a normal person) to step-by, it is generally impossible to graduate from primary school directly to the university, unless he (she) is a genius or after special training. So you ggjjddmm, look at the VCL source code also has a basic pre-problem, first you have to be familiar with WIN32 api/sdk, if you say do not know, you can refer to the book "Programming Windows" (Chinese name "Windows Programming"). Secondly, you should be familiar with Object Pascal, or if you have ever extended the components of Delphi (done component development), then I am sure you are familiar with Object Pascal. Does not know also does not matter, Delphi's online help has to the object Pascal's narration, if the English is too poor also does not matter, the net also has many enthusiastic netizens to translate over the Chinese help and the language reference book.

The topic of this article is the analysis of the VCL source code, analysis of course there is an analysis of the problem, always can not open a source program, catch a function to analyze a function bar:) So we should also have a choice, purposeful analysis. Think about what attributes we'll encounter every day we encode? Oh, name,caption,visible, there are some control TEXT (such as Edit1.text). So let's analyze it with the caption of the control. Of course not every control has a Caption property, and we use the Caption property of the TButton class for analysis. Open every day we will use the Delphi, put a button on form forms, get a Button1 button control, press F12 to open the source program, have not found this code:
Button1:tbutton;
By the right mouse button on the TButton, select the first find Declaration in the popup context menu and find the definition of the TButton class as follows:

TButton = Class (Tbuttoncontrol) Privatefdefault:boolean; Fcancel:boolean; Factive:boolean; Fmodalresult:tmodalresult;procedure SetDefault (Value:boolean); end;

Original TButton inherited from the Tbuttoncontrol class, hehe:)

In the Left Object window (Exploring Unit.pas window), find the Caption property of TButton, such as:
Double-click the Caption property to find the source code that defines the Caption property, and you may find nothing but a
Property Caption;
Hehe, the friends who have written the components know that the caption attribute should have read/write text method ah? Where to go, hehe, here does not appear, of course, it should be in its parent class (here is just to declare caption out of the place), we follow the method of just continue in Tbuttoncontrol, found there is no, finally we found in Tcontrol class this caption, As for why I am a member of protected, I will not say more:

Protectedproperty caption:tcaption Read GetText write SetText stored iscaptionstored;

See GetText, SetText is the function of manipulating text properties, we find GetText, SetText defined as follows:

function gettext:tcaption;
Procedure SetText (const value:tcaption);
And Tcaption, whose definition is actually a custom type:
Tcaption = type string;
Description of the GetText return value and the invocation parameter of SetText, which is also a string type:)

Let's take a look at GetText source code:

Let's take a look at GetText Source: function Tcontrol.gettext:tcaption;var len:integer;begin Len: = gettextlen;//Get text length SetString ( result, PChar (nil), Len);//Set result returns the length specified with Len if Len <> 0 then Gettextbuf (Pointer (Result), Len + 1);//length is not NULL, result Get Text data end;//If you do not understand gettextbuf usage, look at the following code: procedure Tform1.button1click (Sender:tobject); var Buffer:pchar;   Size:byte;begin Size: = Edit1.gettextlen;                 Get EDIT1 's text length Inc (Size);          Getmem (Buffer, Size);  Creates a cache space of EDIT1 text length size edit1.gettextbuf (buffer,size);   The text is obtained from the cache, and the value in buffer is edit1.text edit2.text: = Strpas (buffer);      Buffer converted to Pascal character type data freemem (buffer, Size); Release memory end; The behavior of the above program is equivalent to the following: procedure Tform1.button1click (sender:tobject); begin Edit2.text: = edit1.text;end;// Back to the GetText function, where GetTextLen's function is to get the text length, gettextbuf get the text data. SetText is simpler and is defined as follows: Procedure Tcontrol.settext (const value:tcaption); begin if GetText <> Value then Settextbuf ( PChar (value)); end;//means that if the value set is different from the original, the cached text is reset. To get deeper into the bottom of the VCL, let's look at how GetTextLen is implemented (in fact settextBUF and GetTextLen implementation process similar): function tcontrol.gettextlen:integer;begin Result: = Perform (wm_gettextlength, 0, 0);//wm_ The distribution is the Windows standard message end;//see here presumably everyone understand, if not understand (used Perform), I look at Perform, what it did: function Tcontrol.perform (MSG: Cardinal; WParam, Lparam:longint): Longint;var message:tmessage; begin{your message to Tmessage} message.msg: = MSG;; Message.wparam: = WParam; Message.lparam: = LParam; Message.result: = 0;//0 means return does not handle if self <> nil then WindowProc (message),//Not empty, the message is given to the Tcontrol window procedure WindowProc processing Result: = message.result;//Returns the result end;//here to see what WindowProc did, Tcontrol inside WindowProc is defined as: Property WindowProc: Twndmethod Read Fwindowproc write fwindowproc;//in the Tcontrol Create function: constructor tcontrol.create (aowner:tcomponent); Begin inherited Create (Aowner); Fwindowproc: = wndproc;end;//Visible We also need to find the Tcontrol WndProc process to understand what the WNDPROC process is defined as: Procedure Tcontrol.wndproc (Var Message : Tmessage); var form:tcustomform;   Keystate:tkeyboardstate; Wheelmsg:tcmmousewheel;begin if (csdesigning in Componentstate) theN begin Form: = Getparentform (self); if (Form <> nil) and (Form.designer <> nil) and FORM.DESIGNER.ISDESIGNMSG (self, Message) then Exit end;   if (message.msg >= wm_keyfirst) and (message.msg <= wm_keylast) THEN begin Form: = Getparentform (self); if (Form <> nil) and Form.wantchildkey (self, Message) then Exit; End else if (message.msg >= wm_mousefirst) and (message.msg <= wm_mouselast) THEN BEGIN if not (Csdoubleclicks in ControlStyle) then case message.msg of WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK, Wm_mbuttondblclk:dec (Message .     MSG, Wm_lbuttondblclk-wm_lbuttondown);   End     Case Message.msg of WM_MOUSEMOVE:Application.HintMouseMessage (self, Message); Wm_lbuttondown, wm_lbuttondblclk:begin if Fdragmode = Dmautomatic THEN BEGIN Beginautodra           G         Exit;         End       Include (Fcontrolstate, Cslbuttondown);     End Wm_lbuttonup:exclude (Fcontrolstate, CslbuttondoWN); else with Mouse do if Wheelpresent and (regwheelmessage <> 0) and (message.msg = regwheelmessage)         Then begin Getkeyboardstate (KeyState);           With Wheelmsg do begin MSG: = message.msg;           Shiftstate: = Keyboardstatetoshiftstate (KeyState);           Wheeldelta: = Message.wparam;         Pos: = Tsmallpoint (Message.lparam);         End         Mousewheelhandler (Tmessage (wheelmsg));       Exit;   End End End else If message.msg = Cm_visiblechanged then with Message do Senddocknotification (MSG, WParam, LParam); Dispatch (message);//Dispatch message end;//Here is mainly about the Dispatch method, which invokes the handle method of the message based on the incoming message, if no handle to the message is found in the component class and its parent class,   The Dispatch method calls DefaultHandler (the default message processing method), as follows: Procedure Tobject.dispatch (Var message); ASM PUSH ESI MOV Si,[edx] OR si,si JE @ @default CMP si,0c000h JAE @ @default PUSH EAX MOV eax,[eax] Call getd Ynamethod POP EAX JE @ @default MOV Ecx,esi  Pop esi jmp ecx@ @default: Pop esi MOV ecx,[eax] JMP DWORD PTR [ECX] + vmtoffset Tobject.defa ulthandler//calls the default message processing method end;//and the default message processing is as follows, in the System.pas unit: Procedure Tobject.defaulthandler (Var message); beginend;

As the above code looks like there is no processing process, trace Object.defaulthandler assembly execution action call DWORD ptr[ecx-$10], that is called Object.defaulthandle, to see what to do with:
{Object.defaulthandle}
Ret
Lea eax,[eax+$00]
That is, a return processing!

From the very surface of the button.caption, we go to the compiler layer and see that everything can be found in its native origin! Based on the analysis of caption, we can continue to analyze the name attribute and some other methods/functions. I hope this essay ' prose ' can give you some clues:

Reference: http://blog.csdn.net/iseekcode/article/details/4868883

VCL Source Code Analysis Methodology (taking the origin of the Tbutton.caption attribute as an example)

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.