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.
source share