Author: Tomas Rutkauskas
I think all of us know the function in MS Word which is called "Insert Table".
Pressing this button there appears SubMenu window which is separated in squares -
cells and columns. When we move a mouse on these squares they become active and
after pressing left mouse button is create the table with the same number of cells
and columns as we selected. Maybe anyone could suggest how to do this.
Answer:
The solution is
Put TDrawGrid component on Form and name it dwgTable, then property DefaultDrawing
set to false.
Also configure the following properties:
DefaultColWidth to 20
DefaultRowHeight to 15
ColCount first set to 0 then 3
RowCount first set to 0 then 3
BorderStyle to bsNone
Schroolbar also to none
The code is
1 2 procedure TForm1.dwgTableMouseMove(Sender: TObject; Shift: TShiftState; X,
3 Y: Integer);
4 var5 vCol, vRow: Integer;
6 begin7 if (X > 0) and (y > 0) then8 begin9 vCol := Trunc(x / (dwgTable.DefaultColWidth + 1));
10 vRow := Trunc(y / (dwgTable.DefaultRowHeight + 1));
11 dwgTable.ColCount := vCol + 2;
12 dwgTable.RowCount := vRow + 2;
13 end;
14 dwgTable.Height := (dwgTable.DefaultRowHeight + 1) * dwgTable.RowCount;
15 dwgTable.Width := (dwgTable.DefaultColWidth + 1) * dwgTable.ColCount;
16 end;
17 18 procedure TForm1.dwgTableDrawCell(Sender: TObject; ACol, ARow: Integer;
19 Rect: TRect; State: TGridDrawState);
20 var21 vColText, vRowText: string;
22 RowHorzOffset, RowVertOffset,
23 ColHorzOffset, ColVertOffset: integer;
24 begin25 if (dwgTable.ColCount - 1 = ACol) or (dwgTable.RowCount - 1 = ARow) then26 begin27 dwgTable.Canvas.Brush.Color := clInfoBk;
28 dwgTable.Canvas.FillRect(Rect);
29 vColText := Inttostr(ACol + 1);
30 vRowText := Inttostr(ARow + 1);
31 with dwgTable.Canvas do32 begin33 RowVertOffset := (((Rect.Bottom - Rect.Top) - TextExtent(vRowText).CY)
34 div 2);
35 RowHorzOffset := ((Rect.Right - Rect.Left) - TextExtent(vRowText).CX)
36 div 2;
37 ColVertOffset := (((Rect.Bottom - Rect.Top) - TextExtent(vColText).CY)
38 div 2);
39 ColHorzOffset := ((Rect.Right - Rect.Left) - TextExtent(vColText).CX)
40 div 2;
41 end;
42 43 if (dwgTable.ColCount - 1 <> ACol) or (dwgTable.RowCount - 1 <> ARow) then44 begin45 if (dwgTable.ColCount - 1 = ACol) then46 dwgTable.Canvas.TextOut(Rect.Left + RowhorzOffset, Rect.Top +
47 RowVertOffset, vRowText);
48 if (dwgTable.RowCount - 1 = ARow) then49 dwgTable.Canvas.TextOut(Rect.Left + ColhorzOffset, Rect.Top +
50 ColVertOffset, vColText);
51 end;
52 end53 else54 begin55 dwgTable.Canvas.Brush.Color := clWindow;
56 dwgTable.Canvas.FillRect(Rect);
57 end;
58 end;
59 60 procedure TForm1.dwgTableClick(Sender: TObject);
61 begin62 ShowMessage('Col:' + Inttostr(dwgTable.ColCount - 1) + '
63 Row: 'dwgTable.RowCount-1));
64 end;