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 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 All Versions
Views
134
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I need to align cells in a StringGrid - first cell to the left, second to the right 
and so on ...

Answer:
1   
2   procedure WriteText(ACanvas: TCanvas; const ARect: TRect; DX, DY: Integer;
3     const Text: string; Format: Word);
4   var
5     S: array[0..255] of Char;
6     B, R: TRect;
7   begin
8     with ACanvas, ARect do
9     begin
10      case Format of
11        DT_LEFT:
12          ExtTextOut(Handle, Left + DX, Top + DY, ETO_OPAQUE or ETO_CLIPPED,
13            @ARect, StrPCopy(S, Text), Length(Text), nil);
14        DT_RIGHT:
15          ExtTextOut(Handle, Right - TextWidth(Text) - 3, Top + DY,
16            ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);
17        DT_CENTER:
18          ExtTextOut(Handle, Left + (Right - Left - TextWidth(Text)) div 2, Top + DY,
19            ETO_OPAQUE or ETO_CLIPPED, @ARect, StrPCopy(S, Text), Length(Text), nil);
20      end;
21    end;
22  end;
23  
24  procedure TBEFStringGrid.DrawCell(Col, Row: Longint; Rect: TRect;
25    State: TGridDrawState);
26  var
27    procedure Display(const S: string; Alignment: TAlignment);
28    const
29      Formats: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
30    begin
31      WriteText(Canvas, Rect, 2, 2, S, Formats[Alignment]);
32    end;
33  begin
34    {test the Col and Row arguments here and format the cells as you want}
35    case Row of
36      0: { Center column headings }
37        if (Col < ColCount) then
38          Display(Cells[Col, Row], taCenter)
39        else
40          { Right justify all other entries }
41          Display(Cells[Col, Row], taRight);
42    end;
43  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