Is there any way to center the text vertically in a TEdit without the borders (BorderStyle=bsnone) ?
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.
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;