Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Align cells in a TStringGrid (2) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
05-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
154
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I need to right-align text in certain cells of a TStringGrid. How can I do that?

Answer:

Solve 1:
1   
2   procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
3     Rect: TRect; State: TGridDrawState);
4   var
5     dx: integer;
6     Text: string;
7   begin
8     if aCol in [1..2] then {If column is 1 or 2, then right-align text}
9       with StringGrid1.Canvas do
10      begin
11        FillRect(Rect);
12        Text := StringGrid1.cells[aCol, aRow];
13        dx := TextWidth(Text) + 2;
14        TextOut(Rect.Right - dx, Rect.Top, Text);
15      end;
16  end;



Solve 2:
17  
18  procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
19    Rect: TRect; State: TGridDrawState);
20  const
21    CharOffset = 3;
22  var
23    fLeft: Integer;
24  begin
25    {Right justify column 3}
26    if (ACol = 3) then
27    begin
28      with TStringGrid(Sender) do
29      begin
30        Canvas.FillRect(Rect);
31        fLeft := Rect.Right - Canvas.TextWidth(Cells[ACol, ARow]);
32        Canvas.TextOut(fleft - CharOffset, Rect.Top + CharOffset, Cells[ACol, ARow]);
33      end;
34    end;
35  end;



Solve 3:

The following code aligns the strings of the first column in a TStringGrid to the 
right:
36  
37  procedure DrawTheText(ACanvas: TCanvas; ARect: TRect;
38    AValue: string; AAlign: TAlignment);
39  var
40    horzOffset: integer;
41    options: integer;
42    vertOffset: integer;
43  begin
44    {Note: The Handle property of TCanvas is the handle of its DC.}
45    with ACanvas do
46    begin
47      vertOffset := ARect.Top + (((ARect.Bottom - ARect.Top) - 
48  			TextExtent(AValue).CY) div 2);
49      horzOffset := TextExtent('Mi').CX div 4;
50        {Yields rougly the width of an "average" character}
51      options := ETO_CLIPPED or ETO_OPAQUE;
52      case AAlign of
53        taLeftJustify:
54          begin
55            SetTextAlign(Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP);
56            ExtTextOut(Handle, ARect.Left + horzOffset, vertOffset, options,
57              @ARect, PChar(AValue), Length(AValue), nil);
58          end;
59        taRightJustify:
60          begin
61            SetTextAlign(Handle, TA_RIGHT or TA_TOP or TA_NOUPDATECP);
62            ExtTextOut(Handle, ARect.Right - horzOffset, vertOffset, options,
63              @ARect, PChar(AValue), Length(AValue), nil);
64          end;
65        taCenter:
66          begin
67            horzOffset := ((ARect.Right - ARect.Left) - TextExtent(AValue).CX) div 2;
68            SetTextAlign(Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP);
69            ExtTextOut(Handle, ARect.Left + horzOffset, vertOffset, options,
70              @ARect, PChar(AValue), Length(AValue), nil);
71          end;
72      end;
73    end;
74  end;
75  
76  procedure TFrmAlignText.StringGridDrawCell(Sender: TObject; Col, Row: Integer;
77    Rect: TRect; State: TGridDrawState);
78  var
79    align: TAlignment;
80    theText: string;
81    thisRect: TRect;
82  begin
83    {Assumes that DefaultDrawing := true}
84    theText := IntToStr(Col) + ':' + IntToStr(Row);
85    align := taLeftJustify;
86    {Select background color}
87    if gdSelected in State then
88      StringGrid.Canvas.Brush.Color := clHighlight
89    else if gdFixed in State then
90      StringGrid.Canvas.Brush.Color := clBtnFace
91    else
92      StringGrid.Canvas.Brush.Color := clWindow;
93    {Determine font/text attributes}
94    StringGrid.Canvas.Font.Color := clBlack;
95    StringGrid.Canvas.Font.Style := StringGrid.Canvas.Font.Style - [fsBold];
96    if Col = 1 then
97    begin
98      align := taRightJustify;
99      StringGrid.Canvas.Font.Style := StringGrid.Canvas.Font.Style + [fsBold];
100     StringGrid.Canvas.Font.Color := clRed;
101     StringGrid.Canvas.Font.Name := 'Courier New';
102   end
103   else if Col = 2 then
104   begin
105     align := taCenter;
106     StringGrid.Canvas.Font.Style := StringGrid.Canvas.Font.Style + [fsBold]
107   end
108   else if (Row = 0) and (Col = 0) then
109   begin
110     StringGrid.Canvas.Draw(Rect.Left + 2, Rect.Top + 2, Image.Picture.Graphic);
111   end;
112   if not ((Row = 0) and (Col = 0)) then
113     DrawTheText(StringGrid.Canvas, Rect, theText, align);
114   {Draw focus rectangle}
115   if gdFocused in State then
116     StringGrid.Canvas.DrawFocusRect(Rect);
117   if gdFixed in State then
118     Frame3D(StringGrid.Canvas, Rect, clWindow, clHighlight, 1);
119 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC