TAdvStringGrid: Text alignment in cell

Hello!


I want to align the text in the first line vertical = vtaCenter. 

I do so:

procedure TForm2.AdvStringGrid1GetAlignment(Sender: TObject; ARow,
  ACol: Integer; var HAlign: TAlignment; var VAlign: TVAlignment);
begin
if ARow = 0 then
  begin
    VAlign := vtaCenter;
end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  AdvStringGrid1.RowHeights [0] := 50;
  AdvStringGrid1.Cells[0, 0] := '22';
end;

As a result of this code, there is no alignment = vtaCenter.

How can I make the text alignment vertical vtaCenter?

Version of TAdvStringGrid  = 6.2.7.1

To enable vertically centered alignment, make sure to set grid.WordWrap to false.


Thank you very much, it worked! But where is the logic?

Vertical alignment and wordwrap are mutually exclusive. This is due to a limitation in the Microsoft Windows text drawing API that does not allow to have wordwrapped text with other vertical alignment than top alignment.

Ок, thanks a lot!

I know this is an old thread... What if you want word wrapping enabled - i.e. you have more than line of text displayed in your grid cell - and you also want centered vertical alignment?

This seems to still be an issue. Is it not possible to work around it?

It remains a shortcoming of the GDI DrawText() function that is used internally (for performance reasons):

Would it be possible for your grid to offset the text rect when vaCentre or vaBottom are used with wordwrap ?

With public property grid.XYOffset: TPoint you can change the offset from top/left where text drawing starts

I just tried that, but it just makes the rows larger. It's like moving the top of the text pushed the bottom of the row down.

You asked for a way to offset the text, which is just what it does. Not sure what else you want.

I was hoping for a way to fix the vertical alignment with word wrap on, but I'm guessing the upshot it that it's not possible otherwise TMS would have done it already.

We further reflected on this and as a possible solution, we extended our HTML drawing engine with the ability to take vertical alignment in account.
In the next update, if you add HTML (wordwrapped) formatted text to a cell, it will use the VAlignment property; If your cell text is not really HTML, you can easily make it HTML by just embedding the regular text in <p> </p> tags.

1 Like

Thank you. I'll give it a go once the update is out.

v13.2.0.0 is released

1 Like