Author: Tomas Rutkauskas
Anyone know a simple way of vertically centering your text in a TStringGrid cell.
Actually, I wish the StringGrid had the ability to align horizontally as well.
Answer:
Below is some Delphi3 code that I wrote for handling left-right alignment of text
in string grids. It would be straightforward to change it for vertical instead (or
as well). See DT_BOTTOM, DT_VCENTER and DT_TOP in the description of DrawText in
Delphi's Win32 help file. The code also handles automatic word-wrapping and font
changes on a per cell basis.
1
2 procedure DrawSGCell(Sender: TObject; C, R: integer; Rect: TRect;
3 Style: TFontStyles; Wrap: boolean; Just: TAlignment; NoEditCols: TNoEditCols);
4
5 {formats cell text; call this routine from grid's DrawCell event;
6 Style is TFontStyles...
7 TFontStyles = set of TFontStyle;
8 TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
9 Wrap is word-wrap on/off,
10 Just is (taLeftJustify, taRightJustify, taCenter)}
11
12 var
13 S: string;
14 DrawRect: TRect;
15 begin
16 {multi-line wordwrapped cells, with any justification, and any font params}
17 {if Row > 0 then
18 { only used for column headings}
19 exit;
20 }
21 { get cell contents }
22 with (Sender as TStringGrid), Canvas do
23 begin
24 S := Cells[C, R];
25 {erase earlier contents from default drawing }
26 Brush.Color := FixedColor;
27 if (R >= FixedRows) and (C >= FixedCols) and not (C in NoEditCols) then
28 Brush.Color := Color;
29 FillRect(Rect);
30 if length(S) > 0 then
31 begin
32 {switch to font style}
33 Font.Style := Style;
34 {local copy of cell rectangle}
35 DrawRect := Rect;
36 if Wrap then
37 begin
38 {get size of text rectangle in DrawRect}
39 DrawText(Handle, PChar(S), length(S), DrawRect, dt_calcrect or dt_wordbreak
40 or
41 dt_center);
42 if (DrawRect.Bottom - DrawRect.Top) > RowHeights[R] then
43 begin
44 {cell word-wrapped; need to increase row height}
45 RowHeights[R] := DrawRect.Bottom - DrawRect.Top;
46 SetGridHeight(Sender as TStringGrid);
47 end
48 else
49 begin
50 DrawRect.Right := Rect.Right;
51 FillRect(DrawRect);
52 case Just of
53 taLeftJustify:
54 begin
55 S := ' ' + S;
56 DrawText(Handle, PChar(S), length(S), DrawRect, dt_wordbreak or
57 dt_left);
58 end;
59 taCenter:
60 DrawText(Handle, PChar(S), length(S), DrawRect, dt_wordbreak or
61 dt_center);
62 taRightJustify:
63 begin
64 S := S + ' ';
65 DrawText(Handle, PChar(S), length(S), DrawRect, dt_wordbreak or
66 dt_right);
67 end;
68 end;
69 end;
70 end
71 else
72 {no wrap}
73 case Just of
74 taLeftJustify:
75 begin
76 S := ' ' + S;
77 DrawText(Handle, PChar(S), length(S), DrawRect, dt_singleline or
78 dt_vcenter or dt_left);
79 end;
80 taCenter:
81 DrawText(Handle, PChar(S), length(S), DrawRect, dt_singleline or
82 dt_vcenter or dt_center);
83 taRightJustify:
84 begin
85 S := S + ' ';
86 DrawText(Handle, PChar(S), length(S), DrawRect, dt_singleline or
87 dt_vcenter or dt_right);
88 end;
89 end;
90 {restore no font styles}
91 Font.Style := [];
92 end;
93 end;
94 end;
|