FMX is a set of UI class library, the equivalent of the previous VCL, but compared to VCL, supporting the cross-platform, but also directly inside support a variety of special effects animation or even 3D effect, if the efficiency of the performance up, this class library is still very promising. My main study this time is how a FMX form is drawn and displayed, compared to VCL, what is different, and the most simple analysis of the start-up operation of an FMX program. As for a variety of special effects, animation, and so on, and then slowly to eat, can chew. Create a new Firemonkey HD Desktop application,ide will automatically build a project, into the project, you can find the FMX program, each unit is preceded by the FMX name space to mark, FMX's form, Application and a variety of controls are already rewritten, rather than the set of VCL inheritance system, as for this fmx of the overall inheritance structure, the other has introduced instructions, you can go online search, not recorded here. I am here to mainly analyze the operation and display of a program. Program run, the first thing to see is the Application object, this object in the Fmx.forms, a FMX project to run the simplest engineering code structure forbeginapplication.initialize; Application.createform (TForm1, Form1); Application.Run;Endthis basic code and VCL mode is similar, then the key is the implementation of the internal, because the FMX form is not the previous VCL, so we first look at this createform, this CreateForm code is very interesting, it will be very painfulprocedureTapplication.createform (ConstInstanceclass:tcomponentclass;varReference);varinstance:tcomponent; Registryitems:tformregistryitems; Regitem:tformregistryitem;begin ifFrealcreateformscalled Then beginInstance:=tcomponent (instanceclass.newinstance); Tcomponent (Reference):=Instance; Tryinstance.create (self); forRegiteminchFcreateforms Do ifRegitem.instanceclass = Instanceclass Then beginregitem.instance:=Instance; Regitem.reference:=@Reference; End; excepttcomponent (Reference):=Nil; Raise; End; End Else beginSetLength (fcreateforms, Length (fcreateforms)+1); Fcreateforms[high (fcreateforms)]:=tformregistryitem.create; Fcreateforms[high (fcreateforms)]. Instanceclass:=Instanceclass; Fcreateforms[high (fcreateforms)]. Reference:=@Reference; //Add the form to form registry in case registerformfamily won't be called ifFformregistry.containskey (EMPTYSTR) Then beginRegistryitems:=FFORMREGISTRY[EMPTYSTR]; End Else beginRegistryitems:=tformregistryitems.create; Fformregistry.add (EMPTYSTR, Registryitems); End; Registryitems.add (Fcreateforms[high (fcreateforms)); End;End, how, very interesting, do not know why this is written. This code means that no real creation of the main form will be created until a form registration entry is saved to an internal array of registrations, and then run application calls the Realcreateforms function for form creation. At this point the frealcreateformscalled will be true, The second parameter of the form created with Application.createform is then returned to the actual form object, otherwise without run, using this method does not create the object, that is, we were previously able to write in the project code in VCLbeginapplication.initialize; Application.createform (TForm1, Form1); Form1.caption:='VCL Form';//This code in VCL can, FMX at this time Form1 is not created, so this property assignment error! Application.Run;End. But in the FMX form, we use the Form1 object before run to make an error. This is something to remember. And look at the Run method, this code is very concise.procedureTapplication.run;varAppservice:ifmxapplicationservice;begin{$IFNDEF ANDROID}Addexitproc (doneapplication);{$ENDIF}frunning:=True; Try ifTPlatformServices.Current.SupportsPlatformService (Ifmxapplicationservice, IInterface (Appservice)) ThenAppservice.run; finallyfrunning:=False; End;End; is mainlyifTPlatformServices.Current.SupportsPlatformService (Ifmxapplicationservice, IInterface (Appservice)) Then this conversion, and then call Appservice's run. This is for the platform. Tplatformservices in the Fmx.platform unit, you know that this current is actually a singleton tplatformservices object, and then Supportsplatformservice Ifmxapplicationse Rvice Interface Query transformation. So the supportsplatformservice that was created by God's horse and registered in this tplatformservices, we turned to the initialization at the bottom of the unit, Can be found to call registercoreplatformservices this, this is to register the Platform service interface. , and this function is available in platforms such as Android,windows,ios, For example, Fmx.platform.win,fmx.platform.android, as for the distinction between using that, is to use the compilation preprocessing, to see what the user's target chooses what platform to register what function. Then Windows is Tplatformwin,application is also established when this object is established, you can view his create code, then build Apphandle, use the Createapphandle function, then build the form, Because the only form in FMX is a GDI object with a handle similar to the VCL Wincontrol, so you must use CreateWindow to create the window, then the message agent to application up, in the FMX under win, this is also necessary, So find the corresponding method, is createhandle this, this function is actually called Tplatformwin CreateWindow, and then return a handle, this handle is not a DWORD in the VCL handle value, But a Twindowhandle object. In this process of creating a form, you can find that he directly assigns the message processing process of the form to the WndProc function process, all of which are handled by this process. The middle of the message processing is not said, the following is a form and the control on the form of the drawing display process. Because all the controls on the FMX form display objects that are using the device scene handle of the form itself, we have to look at his drawing display process directly to see the WndProc in the above wm_ The paint message is on the line. Then find the Wmpaint method as follows: functionWmpaint (Hwnd:hwnd; umsg:uint; wparam:wparam; lparam:lparam): LRESULT;stdcall;varI, Rgnstatus:integer; Region:hrgn; Regionsize:integer; Regiondata:prgndata; R:trect; Lform:tcommoncustomform; Updaterects, inpaintupdaterects:tupdaterects; Ps:tpaintstruct; Wnd:Winapi.Windows.HWND; Paintcontrol:ipaintcontrol;beginLform:=FindWindow (HWND); ifLform <>Nil Then beginWnd:=Formtohwnd (Lform); GetUpdateRect (Wnd, R, False); Region:=createrectrgn (R.left, R.top, R.right, R.bottom); ifRegion <>0 Then TryRgnstatus:=Getupdatergn (Wnd, Region, False); if(Rgnstatus =2)or(Rgnstatus =3) Then beginregionsize:= Getregiondata (region, $FFFF,Nil); ifRegionsize >0 Then beginGetmem (Regiondata, regionsize); Tryregionsize:=getregiondata (Region, Regionsize, Regiondata); ifRegionsize = Regionsize Then beginSetLength (updaterects, RegionData.rdh.nCount); forI: =0 toRegionData.rdh.nCount-1 Do beginR:= Prgnrects (@RegionData. buffer[0]) [i]; Updaterects[i]:=RECTF (R.left, R.top, R.right, R.bottom); End; End; finallyFreemem (Regiondata, regionsize); End; ifSupports (Lform, Ipaintcontrol, Paintcontrol) Then beginPaintcontrol.contexthandle:=BeginPaint (Wnd, PS); Try ifPlatformWin.FInPaintUpdateRects.TryGetValue (Lform.handle, inpaintupdaterects) and(Length (inpaintupdaterects) >0) Then begin //add update rects from Finpaintupdaterects forI: =0 toHigh (Inpaintupdaterects) Do beginSetLength (updaterects, Length (updaterects)+1); Updaterects[high (updaterects)]:=Inpaintupdaterects[i]; End; End; Paintcontrol.paintrects (updaterects); ifPlatformWin.FInPaintUpdateRects.TryGetValue (Lform.handle, inpaintupdaterects) and(Length (inpaintupdaterects) >0) Then begin //Paint second time-when Repaint called in paintingPlatformWin.FInPaintUpdateRects.TryGetValue (Lform.handle, updaterects); SetLength (Inpaintupdaterects,0); PlatformWin.FInPaintUpdateRects.AddOrSetValue (Lform.handle, inpaintupdaterects); Paintcontrol.paintrects (updaterects); End; Paintcontrol.contexthandle:=0; finallyEndPaint (Wnd, PS); End; End; End; End; finallyDeleteObject (region); End; Result:=DefWindowProc (hwnd, umsg, WParam, LParam); End ElseResult:=DefWindowProc (hwnd, umsg, WParam, LParam);EndThis code is a little bit long, you can see that if you want to draw the control, basically need to inherit ipaintcontrol this interface, and then draw will call this interface Paintrects method, so we then look at the form of the Paintrects method, In Tcustomform.paintrects, you can see the drawing processing of the controls displayed on all forms from here. Debugging can be found that basically all of the first of the control is a Tstyleobject object, this is mainly for the skin management to draw the skin effects, and then into the control of the drawing, when drawing the control will trigger the Tcontrol paintinternal method. The paintrects of the form executes a pareforpaint function, which is used primarily to prepare the drawing of individual child controls. Then, when executing this method, if Tstyledcontrol executes the Applystylelookup method, it is to draw the appearance. The main purpose of this function is to obtain a skin style control, then set the control size, then insert the child control list as the first item, and then draw the inserted appearance style. This is basically a display concept, such as drawing a button, inserting a button appearance style when drawing, and then drawing the appearance
XE6 FMX Control drawing and display