Author: Tomas Rutkauskas
How to create a multiple line heading in a TStringGrid
Answer:
Here is an example for a TStringGrid that has a multiple line heading with centered
and bold text:
1
2 procedure TForm1.grid1DrawCell(Sender: TObject; Col, Row: Longint;
3 Rect: TRect; State: TGridDrawState);
4 var
5 l_oldalign: word;
6 l_YPos, l_XPos, i: integer;
7 s, s1: string;
8 l_col, l_row: longint;
9 begin
10 l_col := col;
11 l_row := row;
12 with sender as TStringGrid do
13 begin
14 if (l_row = 0) then
15 canvas.font.style := canvas.font.style + [fsbold];
16 if l_row = 0 then
17 begin
18 l_oldalign := settextalign(canvas.handle, ta_center);
19 l_XPos := rect.left + (rect.right - rect.left) div 2;
20 s := cells[l_col, l_row];
21 while s <> '' do
22 begin
23 if pos(#13, s) <> 0 then
24 begin
25 if pos(#13, s) = 1 then
26 s1 := ''
27 else
28 begin
29 s1 := trim(copy(s, 1, pred(pos(#13, s))));
30 delete(s, 1, pred(pos(#13, s)));
31 end;
32 delete(s, 1, 2);
33 end
34 else
35 begin
36 s1 := trim(s);
37 s := '';
38 end;
39 l_YPos := rect.top + 2;
40 canvas.textrect(rect, l_Xpos, l_YPos, s1);
41 inc(rect.top, rowheights[l_row] div 3);
42 end;
43 settextalign(canvas.handle, l_oldalign);
44 end
45 else
46 begin
47 canvas.textrect(rect, rect.left + 2, rect.top + 2, cells[l_col, l_row]);
48 end;
49 canvas.font.style := canvas.font.style - [fsbold];
50 end;
51 end;
|