How to automatically resize TStringGrid row (DefaultRowHeight) to match the height of the font used?
I do something like
Grid.DefaultRowHeight:= Grid.Canvas.TextHeight(‘X’) + 4;
but it is not working. For small fonts, the height of the rows is way too big.
Update:
It seems to be actually a problem with program’s logic. If I change the font multiple times, the current height of the row matches the size of the font from previous font change event (it is one step behind).
I use this code to intercept font’s size change:
procedure TStrGrid.CMFontChanged(var Message: TMessage);
begin
inherited; // let TControl react first
DefaultRowHeight:= Canvas.TextHeight('ApApM')+ 4;
end;
It acts as if the procedure will be:
begin
DefaultRowHeight:= Canvas.TextHeight('ApApM')+ 4;
inherited;
end;
(like first it changes the height, then it actually sets the correct font size – therefore the height is one step behind)
It has nothing to do with this order: both the inherited
CMFontChangedmessage handler and theDefaultRowHeightproperty setter call invalidate. It is due to the current font setting which is not yet updated:Explanation:
Invalidate only flags windows to repaint the window (the grid) somewhere in future. That happens certainly after obtaining the text height. As alternative you can call
Repaintprior to the request for the new text height, but that would result in a double repaint, hence settingCanvas.Font.