I want to shorten a filename to fit in a TEdit, something like
Edit1.Text := MinimizeName(FileName, Edit1.Canvas, Edit1.Width);
Unfortunately this doesn't compile because a TEdit does have a Canvas
property directly. The canvas is needed for its font metrics.
How can I access a TEdit's canvas?
(MinimizeName
is declared in Vcl.FileCtrl.)
You could use TControlCanvas
. You should also take the control's Font into account.
e.g.:
var
Canvas: TControlCanvas;
Canvas := TControlCanvas.Create;
try
Canvas.Control := Edit1;
Canvas.Font.Assign(Edit1.Font);
// Do something with Canvas...
finally
Canvas.Free;
end;
If you want to use this on a TWinControl variable you have to use the usual protected access trick:
type
TWinControlAccess = class(TWinControl);
procedure Test(AWinControl: TWinControl);
var
Canvas: TControlCanvas;
begin
Canvas := TControlCanvas.Create;
try
Canvas.Control := AWinControl;
Canvas.Font.Assign(TWinControlAccess(AWinControl).Font);
// Do something with Canvas...
finally
Canvas.Free;
end;
end;