Recent

Author Topic: [SOLVED]TStringGrid.DrawCell Alignment not working with TextOut  (Read 9602 times)

goldenfox

  • New Member
  • *
  • Posts: 47
Hi guys!

I'm currently exploring lazarus and stumbled upon this code which I can't resolve and would like to ask for your guidance.

I can't figure out how to right align text in a column inside TStringGrid.DrawCell proc. Maybe I just missed something.

Here's a snippet

Quote
procedure TfrmChart.sgChartDrawCell(Sender : TObject; aCol, aRow : Integer;
  aRect : TRect; aState : TGridDrawState);
var
   fBalance : extended = 0;
   rAlign: TTextStyle;
begin
  if aRow > 0 then
  begin
    if (aCol = 1) and (sgChart.Cells[2,aRow] = 'GL')  then
      with sgChart do
      begin
        Canvas.Font.Style := [fsBold];
        Canvas.FillRect(aRect);
        Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, Cells[aCol,aRow]);
      end;

    if (aCol = 3) then
      with sgChart do
      begin
        rAlign := Canvas.TextStyle;
        rAlign.Alignment := taRightJustify;
        Canvas.TextStyle := rAlign;
        Canvas.FillRect(aRect);

        fBalance := StrToFloat(Cells[aCol,aRow]);

        if fBalance < 0 then Canvas.Font.Color := clRed;

        Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, FormatFloat('#,##0.00;(#,##0.00)',fBalance));
      end;
  end;
end;

Additional:
Windows 7 Ultimate 32bit
lazarus 1.1 SVN 36782

Thank's a lot =)
« Last Edit: April 15, 2012, 12:50:00 pm by goldenfox »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933

goldenfox

  • New Member
  • *
  • Posts: 47
Re: TStringGrid.DrawCell Alignment not working
« Reply #2 on: April 15, 2012, 12:49:28 pm »
You can do a lot more using Canvas.TextRect
http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tcanvas.textrect.html
with TTextStyle
http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/ttextstyle.html

Thanks for the hint theo.

It's working with TextRect. But I'm wondering why it's not working with TextOut

Here's what I changed to make it work

Quote
Canvas.TextRect(aRect,aRect.Left - 2, aRect.Top + 2, FormatFloat('#,##0.00;(#,##0.00)',fBalance),rAlign);
« Last Edit: April 15, 2012, 12:52:51 pm by goldenfox »

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: [SOLVED]TStringGrid.DrawCell Alignment not working with TextOut
« Reply #3 on: April 16, 2012, 10:33:45 pm »
But I'm wondering why it's not working with TextOut

Code: [Select]
TTextStyle = packed record
    Alignment : TAlignment;  // TextRect Only: horizontal alignment   

http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/ttextstyle.html
Quote
Alignment : TextRect Only: horizontal alignment


 

TinyPortal © 2005-2018