🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.vcl.components.using.general
14 messages
14 total messages Started by Man T Thu, 19 Jun 2008 02:38
TStringGrid
#50
Author: Man T
Date: Thu, 19 Jun 2008 02:38
9 lines
1062 bytes
I want to right align the number in the stringgrid column by padding empty 
space on the left ,eg.

  23
    3
  11
456

However, I found the TStringGrid automatically center align the numbers. 
Re: TStringGrid
#51
Author: Marc Rohloff [TeamB]
Date: Thu, 19 Jun 2008 14:30
22 lines
1801 bytes
On Thu, 19 Jun 2008 12:38:45 +1000, Man T wrote:

> I want to right align the number in the stringgrid column by padding empty 
> space on the left ,eg.
> 
>   23
>     3
>   11
> 456
> 
> However, I found the TStringGrid automatically center align the numbers.

The string grid will left align not center text, but in most fonts a
space is much narrower than a number which is probably why it looks
centred. 
You could either use a fixed width font (like Courier New) for your
grid or you would have to handle the 'OnDrawCell' event and use the
windows API to right align your text.

-- 
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
Re: TStringGrid
#53
Author: Remy Lebeau (TeamB)
Date: Thu, 19 Jun 2008 18:55
17 lines
1538 bytes
"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:4859c69b@newsgroups.borland.com...

> I want to right align the number in the stringgrid column by
> padding empty space on the left ,eg.

It would be easier to use the OnDrawCell event instead.  Custom-draw the 
cells manually by using the Win32 API DrawText() function to right-align the 
text for you.  No extra padding needed.

> However, I found the TStringGrid automatically center align the numbers.

No, it doesn't.


Gambit 
Re: TStringGrid
#54
Author: Man T
Date: Thu, 19 Jun 2008 23:05
21 lines
1766 bytes
>> I want to right align the number in the stringgrid column by
>> padding empty space on the left ,eg.
>
> It would be easier to use the OnDrawCell event instead.  Custom-draw the 
> cells manually by using the Win32 API DrawText() function to right-align 
> the text for you.  No extra padding needed.


Hi, this is what I have in the event:
procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if ((ACol = 4) or (ACol = 5) or (ACol = 6) or
     (ACol = 8) or (ACol = 9)) and (ARow > 0) then
    DrawText(0, PChar(sgView.Cells[ACol,ARow]), -1, Rect, DT_RIGHT);
end;

However, the text in these column do get right aligned. The text are still 
left aligned.
Any idea why? 
Re: TStringGrid
#55
Author: Remy Lebeau (TeamB)
Date: Thu, 19 Jun 2008 23:58
32 lines
2018 bytes
"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:485ae62e$1@newsgroups.borland.com...

> Hi, this is what I have in the event:
<snip>
> However, the text in these column do get right aligned.
> The text are still left aligned.
> Any idea why?

You are not using DrawText() correctly.  For one thing, you ae not passing 
the TStringGrid's Canvas to the function so it knows what to draw on. Try 
this code instead:

    procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol, ARow: Integer; 
Rect: TRect; State: TGridDrawState);
    var
        uiFormat: UINT;
    begin
        sg1.Canvas.FillRect(Rect);

        if ((ACol >= 4) and (ACol <= 9) and (ACol <> 7)) and (ARow > 0) then
            uiFormat := DT_RIGHT
        else
            uiFormat := DT_LEFT;

        Windows.DrawText(sg1.Canvas.Handle, PChar(sgView.Cells[ACol, 
ARow]), -1, Rect, uiFormat);
    end;


Gambit 
Re: TStringGrid
#56
Author: Man T
Date: Fri, 20 Jun 2008 00:45
23 lines
1891 bytes
Cool! It works.
Thanks very much.

> You are not using DrawText() correctly.  For one thing, you ae not passing 
> the TStringGrid's Canvas to the function so it knows what to draw on. Try 
> this code instead:
>
>    procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol, ARow: Integer; 
> Rect: TRect; State: TGridDrawState);
>    var
>        uiFormat: UINT;
>    begin
>        sg1.Canvas.FillRect(Rect);
>
>        if ((ACol >= 4) and (ACol <= 9) and (ACol <> 7)) and (ARow > 0) 
> then
>            uiFormat := DT_RIGHT
>        else
>            uiFormat := DT_LEFT;
>
>        Windows.DrawText(sg1.Canvas.Handle, PChar(sgView.Cells[ACol, 
> ARow]), -1, Rect, uiFormat);
>    end;
Re: TStringGrid
#57
Author: Man T
Date: Fri, 20 Jun 2008 05:13
37 lines
2281 bytes
Hi,
I found the number is right aligned but at the top of the cell.
But the other cell align the 'text' at the vertical center. How do I align 
the number also at the right and center of vertical?
I tried
Windows.DrawText(sgView.Canvas.Handle, PChar(sgView.Cells[ACol, ARow]), -1, 
Rect, DT_RIGHT + DT_VCENTER);

But no luck.

"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:485afda3$1@newsgroups.borland.com...
> Cool! It works.
> Thanks very much.
>
>> You are not using DrawText() correctly.  For one thing, you ae not 
>> passing the TStringGrid's Canvas to the function so it knows what to draw 
>> on. Try this code instead:
>>
>>    procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol, ARow: Integer; 
>> Rect: TRect; State: TGridDrawState);
>>    var
>>        uiFormat: UINT;
>>    begin
>>        sg1.Canvas.FillRect(Rect);
>>
>>        if ((ACol >= 4) and (ACol <= 9) and (ACol <> 7)) and (ARow > 0) 
>> then
>>            uiFormat := DT_RIGHT
>>        else
>>            uiFormat := DT_LEFT;
>>
>>        Windows.DrawText(sg1.Canvas.Handle, PChar(sgView.Cells[ACol, 
>> ARow]), -1, Rect, uiFormat);
>>    end;
>
> 
Re: TStringGrid
#58
Author: Remy Lebeau (TeamB)
Date: Fri, 20 Jun 2008 06:53
26 lines
1924 bytes
"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:485b3c68$1@newsgroups.borland.com...

> How do I align the number also at the right and center of vertical?

Specify the VT_VCENTER and DT_SINGLELINE flags, ie:

    procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol, ARow: Integer; 
Rect: TRect; State: TGridDrawState);
    var
        uiDirection: UINT;
    begin
        sg1.Canvas.FillRect(Rect);

        if ((ACol >= 4) and (ACol <= 9) and (ACol <> 7)) and (ARow > 0) then
            uiDirection := DT_RIGHT
        else
            uiDirection := DT_LEFT;

        Windows.DrawText(sg1.Canvas.Handle, PChar(sgView.Cells[ACol, 
ARow]), -1, Rect, uiDirection or DT_SINGLELINE or DT_VCENTER);
    end;


Gambit 
Re: TStringGrid
#59
Author: Man T
Date: Fri, 20 Jun 2008 07:24
27 lines
1976 bytes
>> How do I align the number also at the right and center of vertical?
>
> Specify the VT_VCENTER and DT_SINGLELINE flags, ie:
>
>    procedure TViewOrder.sg1DrawCell(Sender: TObject; ACol, ARow: Integer; 
> Rect: TRect; State: TGridDrawState);
>    var
>        uiDirection: UINT;
>    begin
>        sg1.Canvas.FillRect(Rect);
>
>        if ((ACol >= 4) and (ACol <= 9) and (ACol <> 7)) and (ARow > 0) 
> then
>            uiDirection := DT_RIGHT
>        else
>            uiDirection := DT_LEFT;
>
>        Windows.DrawText(sg1.Canvas.Handle, PChar(sgView.Cells[ACol, 
> ARow]), -1, Rect, uiDirection or DT_SINGLELINE or DT_VCENTER);
>    end;

Hi,

Sorry I am not sure what I missed.
I used the 'or' as you mentioned it still aligned top and right.
I used DT_RIGHT or  DT_VCENTER 
Re: TStringGrid
#61
Author: Remy Lebeau (TeamB)
Date: Fri, 20 Jun 2008 17:05
10 lines
1396 bytes
"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:485b5b23$1@newsgroups.borland.com...

> Sorry I am not sure what I missed.

You missed the DT_SINGLELINE flag.


Gambit 
Re: TStringGrid
#64
Author: Man T
Date: Sun, 22 Jun 2008 11:21
5 lines
1367 bytes
>> Sorry I am not sure what I missed.
>
> You missed the DT_SINGLELINE flag.

Thanks for your solution! 
Re: TStringGrid
#68
Author: Man T
Date: Sun, 22 Jun 2008 23:12
16 lines
1707 bytes
>> You missed the DT_SINGLELINE flag.
>
> Thanks for your solution!

Hi,

I can now right aligned the text but I found the text is a little bit above 
the other columns if I use
Windows.DrawText(aCanvas.Handle, PChar(aText), -1, aRect, DT_RIGHT or 
DT_SINGLELINE);

but it will a little bit below the other columns if use:
Windows.DrawText(aCanvas.Handle, PChar(aText), -1, aRect, DT_RIGHT or 
DT_SINGLELINE or DT_VCENTER);

Aything I can do? 
Re: TStringGrid
#70
Author: Remy Lebeau (TeamB)
Date: Mon, 23 Jun 2008 05:03
10 lines
1449 bytes
"Man T" <alanpltse_NOSPAM@yahoo.com.au> wrote in message 
news:485edc3c$1@newsgroups.borland.com...

> Aything I can do?

Adjust the TRect that you pass to DrawText()


Gambit 
Re: TStringGrid
#71
Author: Bill Edwards
Date: Mon, 23 Jun 2008 07:41
49 lines
2618 bytes
For example:

procedure TRateMainForm.GrdCGSchedDrawCell(Sender: TObject; ACol, ARow: 
Longint;
  ARect: TRect; AState: TGridDrawState);
begin
  GrdCGSched.Canvas.Font.Style := [];
  If (GrdCGSched.Focused) and (ARow = GrdCGSched.Row) then
    begin
      GrdCGSched.Canvas.Font.Color  := clHighlightText;
      GrdCGSched.Canvas.Brush.Color := clHighlight;
    end
  else
    begin
      GrdCGSched.Canvas.Font.Color  := clWindowText;
      GrdCGSched.Canvas.Brush.Color := GrdCGSched.Color;
    end;
  // ARow[0] is fixed; ACol[0] contains Indicator graphic;
  // ACol[1] contains check box.
  If (ARow > 0) and (ACol > 1) then
    begin
      If (ACol in [2, 7, 8, 9, 11]) then
        begin
          SetTextAlign(GrdCGSched.Canvas.Handle, TA_RIGHT + TA_TOP);
          GrdCGSched.Canvas.TextRect(ARect, ARect.Right - 2, ARect.Top + 2,
            GrdCGSched.Cells[ACol, ARow]);
        end
      else
        begin
          SetTextAlign(GrdCGSched.Canvas.Handle, TA_LEFT + TA_TOP);
          GrdCGSched.Canvas.TextRect(ARect, ARect.Left + 2, ARect.Top + 2,
            UpperCase(GrdCGSched.Cells[ACol, ARow]));
        end;
    end
  else
  If (ACol = 1) and (ARow > 0) then
    DrawGridCheckBox(GrdCGSched, GridImgList, GrdCGSched.CellRect(ACol, 
ARow), 2,
      True {IsChecked[ARow]}, True {GrdCGSched.Focused and (gdSelected in 
AState)});
  If (ACol = 0) and (ARow > 0) then
    begin
      DrawIndicator(GrdCGSched, ARect, (gdSelected in AState),
        Ord(TLiabLine(CGLine[GrdCGSched.Row - 1]^).DataState));
      GrdCGSched.Canvas.Brush.Color := GrdCGSched.FixedLineColor;
      GrdCGSched.Canvas.FrameRect(ARect);
    end;
end;
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads