Forum > LCL

Center the text vertically in a TEdit

(1/1)

pmesquita:
Guys,
Is there any way to center the text vertically in a TEdit without the borders (BorderStyle=bsnone) ?
Because without the borders the text is always on top of the control...
I've been reading that the Windows message WM_NCCALCSIZE changes the internal area of TEdit but this message is simply not sent to TEdit..

Windows Message Post
https://forum.lazarus.freepascal.org/index.php?topic=24595.0

ASerge:

--- Quote from: pmesquita on June 28, 2022, 04:08:23 am ---Is there any way to center the text vertically in a TEdit without the borders (BorderStyle=bsnone) ?

--- End quote ---
It's easier to put TEdit on a TPanel with a clWindow color without borders and the desired size. And then vertically align TEdit inside TPanel.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Windows; procedure VCenterControlInsideParentByTextHeight(Control: TWinControl);   function GetWindowTextHeight(Wnd: HWND): Integer;  var    DC: HDC;    TM: TTextMetric;  begin    DC := GetWindowDC(Wnd);    try      if not GetTextMetrics(DC, TM) then        RaiseLastOSError;    finally      ReleaseDC(Wnd, DC);    end;    Result := TM.tmHeight;  end; var  R: TRect;  Extra: Integer;begin  R := Control.BoundsRect;  Extra := R.Height - GetWindowTextHeight(Control.Handle);  if Extra <= 0 then    Exit;  R.Inflate(0, -Extra div 2);  Control.BoundsRect := R;end; procedure TForm1.Button1Click(Sender: TObject);begin  VCenterControlInsideParentByTextHeight(Edit1);end;

Navigation

[0] Message Index

Go to full version