Hello,
Reading Mr. Elliott reference I have created the Insert Table grid.
The solution is
Put DrawGrid on Form and name it dwgTable, then DefaultDrawing set to false
DefaultColWidth to 20
DefaultRowHeight to 15
ColCount 3
RowCount 3
BorderStyle to bsNone
Schroolbar also to none
The code is
procedure TForm1.dwgTableMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var vCol, vRow: Integer;
begin
if (X > 0) and (y > 0) then
begin
vCol := Trunc(x / (dwgTable.DefaultColWidth + 1));
vRow := Trunc(y / (dwgTable.DefaultRowHeight + 1));
dwgTable.ColCount := vCol + 2;
dwgTable.RowCount := vRow + 2;
end;
dwgTable.Height := (dwgTable.DefaultRowHeight + 1) * dwgTable.RowCount;
dwgTable.Width := (dwgTable.DefaultColWidth + 1) * dwgTable.ColCount;
end;
procedure TForm1.dwgTableDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var vColText, vRowText: string;
RowHorzOffset, RowVertOffset,
ColHorzOffset, ColVertOffset: integer;
begin
if (dwgTable.ColCount - 1 = ACol) or (dwgTable.RowCount - 1 = ARow) then
begin
dwgTable.Canvas.Brush.Color := clInfoBk;
dwgTable.Canvas.FillRect(Rect);
vColText := Inttostr(ACol + 1);
vRowText := Inttostr(ARow + 1);
with dwgTable.Canvas do
begin
RowVertOffset := (((Rect.Bottom - Rect.Top) - TextExtent(vRowText).CY)
div 2);
RowHorzOffset := ((Rect.Right - Rect.Left) - TextExtent(vRowText).CX)
div 2;
ColVertOffset := (((Rect.Bottom - Rect.Top) - TextExtent(vColText).CY)
div 2);
ColHorzOffset := ((Rect.Right - Rect.Left) - TextExtent(vColText).CX)
div 2;
end;
if (dwgTable.ColCount - 1 <> ACol) or (dwgTable.RowCount - 1 <> ARow)
then
begin
if (dwgTable.ColCount - 1 = ACol) then
dwgTable.Canvas.TextOut(Rect.Left + RowhorzOffset, Rect.Top +
RowVertOffset, vRowText);
if (dwgTable.RowCount - 1 = ARow) then
dwgTable.Canvas.TextOut(Rect.Left + ColhorzOffset, Rect.Top +
ColVertOffset, vColText);
end;
end
else
begin
dwgTable.Canvas.Brush.Color := clWindow;
dwgTable.Canvas.FillRect(Rect);
end;
end;
procedure TForm1.dwgTableClick(Sender: TObject);
begin
ShowMessage('Col:'+Inttostr(dwgTable.ColCount-1)+'
Row:'+Inttostr(dwgTable.RowCount-1));
end;
p.s. If you will find bugs then let me know.
Best Regards,
Tomas
Post by Elliott ShevinPost by Jonas BilinkeviciusWhen 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.
I'll take a whack at this, although I'm not by my Delphi box
and can't be specific. I'll describe how I'd duplicate what
Word does.
Define a panel with a TDrawGrid on it, where the cells
are square. Keep it invisible until the user clicks the
button. Add a TStatusBar, aligned to the bottom, where
you'll display the status messages: selected dimensions of
the table, or "Cancel" if the mouse moves outside the grid.
When the user presses the button, set the number of rows
and columns of the TDrawgrid to whatever default values
you like. Size the TPanel to just accommodate the TDrawGrid,
for appearance. Color all the cells white. Position the
TPanel next to the button, and make it visible. Have the
TDrawGrid capture the mouse, so you'll know if it's clicked
while outside the grid.
Then depend on the TDrawGrid's OnMouseMove to determine your
actions.
When the user moves the mouse into a cell, color it
and all cells above and to the left blue, and all remaining
cells white. Display the selected dimensions in the
TStatusBar. You may have to set Doublebuffered := true
on the TPanel to prevent flicker.
When the cursor moves over the right border, add another column
to the TDrawGrid. When it moves over the bottom border, add
another row. (Expand the TPanel accordingly in either case.)
When it moves to the left or bottom border, revert all the
cells to white, and display "Cancel" in the TStatusBar.
When the user clicks, release the mouse capture, and create
the table based on whichever cell it's positioned over,
if any; if it's outside the TDrawGrid, don't. And make the
TPanel invisible again.
You probably should respond to key presses, too, letting the
user expand and contract the table size via the arrow keys,
and cancel via Escape.
Good luck,
Elliott