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;
|