With the following components, you can automatically center vertically when you enter text
Save the following code directly to Unit1.pas
------------------------------------------
1 unit Unit1;2 3 Interface4 5 uses6 Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,7 Dialogs, Stdctrls;8 9 typeTenTedit =Class (Stdctrls.tedit) One protected A procedureCreateParams (varparams:tcreateparams); Override; - procedureKeyPress (varKey:char); Override; - procedureWmsize (varmsg:twmsize); message wm_size; the procedureSetParent (Aparent:twincontrol); override; - procedureSetCenter; - End; -TForm1 =Class (Tform) + Button1:tbutton; - Edit1:tedit; + procedureformcreate (sender:tobject); A Private at {Private Declarations} - Public - {Public Declarations} - Edt:tedit; - End; - in var - Form1:tform1; to + Implementation - the {$R *.DFM} * {Tedit} $ Panax Notoginseng proceduretform1.formcreate (sender:tobject); - begin theEDT: =tedit.create (self); +Edt.parent: =Self ; AEdt.autosize: =False; theEdt.height: = -; + End; - $ procedureTedit.createparams (varparams:tcreateparams); $ begin - inherited; -Params.style: = Params.styleorEs_multiline; the End; - Wuyi procedureTedit.keypress (varKey:char); the begin - inherited; Wu ifKey = # - Then -Key: = #0; About End; $ - procedureTedit.wmsize (varmsg:twmsize); - begin - inherited; A SetCenter; + End; the - proceduretedit.setparent (Aparent:twincontrol); $ begin the inherited; the ifParent <>Nil Then the begin the SetCenter; - End; in End; the the procedureTedit.setcenter; About var the Dc:hdc; the Savefont:hfont; the Sin:integer; + SysMetrics, metrics:ttextmetric; - Rct:trect; the beginBayiDC: = GetDC (0); the gettextmetrics (DC, sysmetrics); theSavefont: =SelectObject (DC, font.handle); - gettextmetrics (DC, Metrics); - SelectObject (DC, savefont); theReleaseDC (0, DC); the ifCtl3d ThenSin: =8 ElseSin: =6; theRCT: =Clientrect; theSin: = Height-metrics.tmheight-Sin; -Rct.top: = SinDiv 2; theSendMessage (Handle, Em_setrect,0, Integer (@Rct)); the End; the 94 the the the End.
When this is saved to Unit1.pas, and then through the Delphi Component installation function to install the components, the specific installation method can go to the online search method
Delphi7 How to enable Tedit display text to center vertically (up and down center)