TA_CENTER does not work to center StringGrid

I need to center the alignment of the text in the StringGrid (its cells), and I use the code that you see here. I found this in another answer here, and I edited something.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); var LStrCell: string; LRect: TRect; qrt:double; begin LStrCell := StringGrid1.Cells[ACol, ARow]; StringGrid1.Canvas.FillRect(aRect); LRect := aRect; DrawText(StringGrid1.Canvas.Handle, PChar(LStrCell), Length(LStrCell), LRect, TA_CENTER); //other code end; 

I use Lazarus and it gives me an error because it does not recognize TA_CENTER . Any solutions?

+4
source share
1 answer

Since you are using Lazarus, I would not rely on the platform-specific Windows API function, but instead used the built-in TextRect canvas. The (unverified) code can be:

 procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); var CellText: string; TextStyle: TTextStyle; begin CellText := StringGrid1.Cells[ACol, ARow]; StringGrid1.Canvas.FillRect(ARect); TextStyle := StringGrid1.Canvas.TextStyle; TextStyle.Alignment := taCenter; StringGrid1.Canvas.TextRect(ARect, 0, 0, CellText, TextStyle); ... end; 

Anyway, you used the TA_CENTER constant, which is used by another Windows API function, using the SetTextAlign function. You must use the DT_ used by the DrawText function.

+5
source

Source: https://habr.com/ru/post/1496160/


All Articles