O
oly102
Unregistered / Unconfirmed
GUEST, unregistred user!
我想把StringGrid当容器,动态创建多个TShape控件,请问怎么使TShape控件能够随着StringGrid的滚动条而滚动。其中标题合并也不理想~
请各位大侠帮忙啊
//新建一工程,在FORM1上放一StringGRID,改名GanttGrid,行个数设为多行到出现滚动条~
//2个DateTimePicker控件,2个Button
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons,ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
GanttGrid: TStringGrid;
BitBtn1: TBitBtn;
Button1: TButton;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure GanttGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormShow(Sender: TObject);
procedure GanttGridTopLeftChanged(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure MyCreateShape(sLabelCaption:String;iLeft,iTop,iWidth,iHeight:integer);
procedure MyCreateLabel(sContent:string;iLeft,iTop:integer);
procedure FillGanttGridTitle(StarDate,EndDate:string);
procedure ClearDynamicComponet(sGrid : TStringGrid);
{ Public declarations }
end;
var
Form1: TForm1;
bFillGrid:boolean;
implementation
{$R *.dfm}
procedure TForm1.ClearDynamicComponet(sGrid : TStringGrid);
//清除动态控件
var i,j : integer;
begin
{ with sGrid do
begin
for i := 0 to ComponentCount- 1 do
begin
if Components is TShape then
TShape(Components).Free;
if Components is TLabel then
TShape(Components).Free;
end;
end; }
with sGrid do
begin
j:=0;
for i := 0 to ComponentCount- 1 do
begin
if Components[j] is TShape then
begin
TShape(Components[j]).Free;
j:=j-1;
end
else if Components[j] is TLabel then
begin
TShape(Components[j]).Free;
j:=j-1;
end;
j:=j+1;
end;
end;
end;
//-----------------------------------------------------------------
procedure TForm1.FillGanttGridTitle(StarDate,EndDate:string);
var tmpDate,eDate:TDate;
i:integer;
begin
if (StarDate='') or (EndDate='') then exit;
try
tmpDate:=strToDate(StarDate);
except
Application.MessageBox('日期错误','提示;',MB_OK);
exit;
end;
try
eDate:=strToDate(EndDate)+1;
except
Application.MessageBox('日期错误','提示',MB_OK);
exit;
end;
GanttGrid.ColCount:=Round((eDate-tmpDate)*6)+1;
i:=0;
while tmpDate<eDate do
begin
GanttGrid.Cells[i*6+0,1]:='4';
GanttGrid.Cells[i*6+1,1]:='8';
GanttGrid.Cells[i*6+2,1]:='12';
GanttGrid.Cells[i*6+3,1]:='16';
GanttGrid.Cells[i*6+4,1]:='20';
GanttGrid.Cells[i*6+5,1]:='24';
GanttGrid.ColWidths[i*6+0]:=30;
GanttGrid.ColWidths[i*6+1]:=30;
GanttGrid.ColWidths[i*6+2]:=30;
GanttGrid.ColWidths[i*6+3]:=30;
GanttGrid.ColWidths[i*6+4]:=30;
GanttGrid.ColWidths[i*6+5]:=30;
GanttGrid.ColWidths[0]:=30;
i:=i+1;
tmpDate:=tmpDate+1;
end;
GanttGrid.ColWidths[i*6]:=-1;
end;
//---------------------------------------------------------------------
procedure TForm1.GanttGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var iRect : TRect;
Xi,Yi ,iCol,iColCount,iColWidth,m,n,iday,iRow: Integer;
iStr : String;
tmpDate,eDate:TDate;
begin
if not bFillGrid then exit;
With TStringGrid(Sender) Do
begin
// GanttGrid.Cells[0,ARow+1]:=inttostr(ARow+1);
iColCount:=6;
iColWidth:=30;
ColWidths[ACol]:=30;
tmpDate:=DateTimePicker1.Date;
eDate:=DateTimePicker2.Date;
iday:=Round(eDate-tmpDate+1);
if iDay<=0 then exit;
n:=(ACol div iColCount);
m:=(ACol mod iColCount);
if m<>0 then n:=n+1;
if n>iDay then exit;
iStr:=DateToStr(tmpDate+n-1);
iRect.Left := CellRect(ACol-m,0).Left;
iRect.Top := CellRect(ACol,0).Top;
iRect.Right := CellRect(ACol,0).Right;
iRect.Bottom :=CellRect(ACol,0).Bottom ;
canvas.Font.Size :=11; &Ograve;&raquo;&micro;&atilde;&iexcl;&pound;
canvas.Font.Color:=clBlue;
canvas.Font.Style:=[fsBold];
Canvas.Brush.Color:=clMenu;
Canvas.FillRect(iRect);
Xi := (iRect.Right-iRect.Left-Canvas.TextWidth(iStr)) div 2;
Xi := iRect.Left+Xi;
Yi := 1;
if (iColCount-m)=1 then
Canvas.TextOut(Xi,Yi,iStr);
iRow:=3;
iCol:=2;
if (ARow=iRow) and (ACol=iCol) then
MyCreateShape('任务1耗时20小时',CellRect(iCol,iRow).Left,GanttGrid.RowHeights[1]*iRow+6+iRow,155,12);
iCol:=5;
iRow:=8;
if (ARow=iRow) and (ACol=iCol) then
MyCreateShape('任务2耗时16小时',CellRect(iCol,iRow).Left,RowHeights[1]*iRow+6+iRow,124,12);
end;
end;
//------------------------------------------------------
procedure TForm1.FormShow(Sender: TObject);
begin
bFillGrid:=false;
end;
//------------------------------------------------------
procedure TForm1.GanttGridTopLeftChanged(Sender: TObject);
begin
GanttGrid.Refresh;
end;
//------------------------------------------------------
procedure TForm1.MyCreateLabel(sContent:string;iLeft,iTop:integer);
var myLabel:TLabel;
begin
myLabel:=TLabel.Create(GanttGrid);
myLabel.Parent:=GanttGrid;
myLabel.Left:=iLeft;
myLabel.Top:=iTop;
myLabel.Caption:=sContent;
// myLabel.AutoSize:=true;
myLabel.Width:=Length(sContent)*7;
myLabel.Transparent:=true;
end;
//----------------------------------------------------------------------
procedure TForm1.MyCreateShape(sLabelCaption:String;iLeft,iTop,iWidth,iHeight:integer);
var myShape:TShape;
begin
myShape:=TShape.Create(GanttGrid);
myShape.Parent:=GanttGrid;
myShape.Left:=iLeft;
myShape.Top:=iTop;
mySHape.Width:=iWidth;
myShape.Height:=iHeight;
myShape.Brush.Color:=clred;
MyCreateLabel(sLabelCaption,iLeft+iWidth+2,iTop);
end;
//----------------------------------------------------------------------
procedure TForm1.BitBtn1Click(Sender: TObject);
var iRow:integer ;
begin
bFillGrid:=true;
FillGanttGridTitle(DateToStr(DateTimePicker1.Date),DateToStr(DateTimePicker2.Date));
// MyCreateShape('任务1耗时20小时',CellRect(iCol,iRow).Left,GanttGrid.RowHeights[1]*iRow+6+iRow,155,12);
// MyCreateShape('任务2耗时16小时',CellRect(iCol,iRow).Left,RowHeights[1]*iRow+6+iRow,124,12);
end;
//----------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
ClearDynamicComponet(GanttGrid);
end;
//----------------------------------------------------------------------
end.
请各位大侠帮忙啊
//新建一工程,在FORM1上放一StringGRID,改名GanttGrid,行个数设为多行到出现滚动条~
//2个DateTimePicker控件,2个Button
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons,ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
GanttGrid: TStringGrid;
BitBtn1: TBitBtn;
Button1: TButton;
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure GanttGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormShow(Sender: TObject);
procedure GanttGridTopLeftChanged(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure MyCreateShape(sLabelCaption:String;iLeft,iTop,iWidth,iHeight:integer);
procedure MyCreateLabel(sContent:string;iLeft,iTop:integer);
procedure FillGanttGridTitle(StarDate,EndDate:string);
procedure ClearDynamicComponet(sGrid : TStringGrid);
{ Public declarations }
end;
var
Form1: TForm1;
bFillGrid:boolean;
implementation
{$R *.dfm}
procedure TForm1.ClearDynamicComponet(sGrid : TStringGrid);
//清除动态控件
var i,j : integer;
begin
{ with sGrid do
begin
for i := 0 to ComponentCount- 1 do
begin
if Components is TShape then
TShape(Components).Free;
if Components is TLabel then
TShape(Components).Free;
end;
end; }
with sGrid do
begin
j:=0;
for i := 0 to ComponentCount- 1 do
begin
if Components[j] is TShape then
begin
TShape(Components[j]).Free;
j:=j-1;
end
else if Components[j] is TLabel then
begin
TShape(Components[j]).Free;
j:=j-1;
end;
j:=j+1;
end;
end;
end;
//-----------------------------------------------------------------
procedure TForm1.FillGanttGridTitle(StarDate,EndDate:string);
var tmpDate,eDate:TDate;
i:integer;
begin
if (StarDate='') or (EndDate='') then exit;
try
tmpDate:=strToDate(StarDate);
except
Application.MessageBox('日期错误','提示;',MB_OK);
exit;
end;
try
eDate:=strToDate(EndDate)+1;
except
Application.MessageBox('日期错误','提示',MB_OK);
exit;
end;
GanttGrid.ColCount:=Round((eDate-tmpDate)*6)+1;
i:=0;
while tmpDate<eDate do
begin
GanttGrid.Cells[i*6+0,1]:='4';
GanttGrid.Cells[i*6+1,1]:='8';
GanttGrid.Cells[i*6+2,1]:='12';
GanttGrid.Cells[i*6+3,1]:='16';
GanttGrid.Cells[i*6+4,1]:='20';
GanttGrid.Cells[i*6+5,1]:='24';
GanttGrid.ColWidths[i*6+0]:=30;
GanttGrid.ColWidths[i*6+1]:=30;
GanttGrid.ColWidths[i*6+2]:=30;
GanttGrid.ColWidths[i*6+3]:=30;
GanttGrid.ColWidths[i*6+4]:=30;
GanttGrid.ColWidths[i*6+5]:=30;
GanttGrid.ColWidths[0]:=30;
i:=i+1;
tmpDate:=tmpDate+1;
end;
GanttGrid.ColWidths[i*6]:=-1;
end;
//---------------------------------------------------------------------
procedure TForm1.GanttGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var iRect : TRect;
Xi,Yi ,iCol,iColCount,iColWidth,m,n,iday,iRow: Integer;
iStr : String;
tmpDate,eDate:TDate;
begin
if not bFillGrid then exit;
With TStringGrid(Sender) Do
begin
// GanttGrid.Cells[0,ARow+1]:=inttostr(ARow+1);
iColCount:=6;
iColWidth:=30;
ColWidths[ACol]:=30;
tmpDate:=DateTimePicker1.Date;
eDate:=DateTimePicker2.Date;
iday:=Round(eDate-tmpDate+1);
if iDay<=0 then exit;
n:=(ACol div iColCount);
m:=(ACol mod iColCount);
if m<>0 then n:=n+1;
if n>iDay then exit;
iStr:=DateToStr(tmpDate+n-1);
iRect.Left := CellRect(ACol-m,0).Left;
iRect.Top := CellRect(ACol,0).Top;
iRect.Right := CellRect(ACol,0).Right;
iRect.Bottom :=CellRect(ACol,0).Bottom ;
canvas.Font.Size :=11; &Ograve;&raquo;&micro;&atilde;&iexcl;&pound;
canvas.Font.Color:=clBlue;
canvas.Font.Style:=[fsBold];
Canvas.Brush.Color:=clMenu;
Canvas.FillRect(iRect);
Xi := (iRect.Right-iRect.Left-Canvas.TextWidth(iStr)) div 2;
Xi := iRect.Left+Xi;
Yi := 1;
if (iColCount-m)=1 then
Canvas.TextOut(Xi,Yi,iStr);
iRow:=3;
iCol:=2;
if (ARow=iRow) and (ACol=iCol) then
MyCreateShape('任务1耗时20小时',CellRect(iCol,iRow).Left,GanttGrid.RowHeights[1]*iRow+6+iRow,155,12);
iCol:=5;
iRow:=8;
if (ARow=iRow) and (ACol=iCol) then
MyCreateShape('任务2耗时16小时',CellRect(iCol,iRow).Left,RowHeights[1]*iRow+6+iRow,124,12);
end;
end;
//------------------------------------------------------
procedure TForm1.FormShow(Sender: TObject);
begin
bFillGrid:=false;
end;
//------------------------------------------------------
procedure TForm1.GanttGridTopLeftChanged(Sender: TObject);
begin
GanttGrid.Refresh;
end;
//------------------------------------------------------
procedure TForm1.MyCreateLabel(sContent:string;iLeft,iTop:integer);
var myLabel:TLabel;
begin
myLabel:=TLabel.Create(GanttGrid);
myLabel.Parent:=GanttGrid;
myLabel.Left:=iLeft;
myLabel.Top:=iTop;
myLabel.Caption:=sContent;
// myLabel.AutoSize:=true;
myLabel.Width:=Length(sContent)*7;
myLabel.Transparent:=true;
end;
//----------------------------------------------------------------------
procedure TForm1.MyCreateShape(sLabelCaption:String;iLeft,iTop,iWidth,iHeight:integer);
var myShape:TShape;
begin
myShape:=TShape.Create(GanttGrid);
myShape.Parent:=GanttGrid;
myShape.Left:=iLeft;
myShape.Top:=iTop;
mySHape.Width:=iWidth;
myShape.Height:=iHeight;
myShape.Brush.Color:=clred;
MyCreateLabel(sLabelCaption,iLeft+iWidth+2,iTop);
end;
//----------------------------------------------------------------------
procedure TForm1.BitBtn1Click(Sender: TObject);
var iRow:integer ;
begin
bFillGrid:=true;
FillGanttGridTitle(DateToStr(DateTimePicker1.Date),DateToStr(DateTimePicker2.Date));
// MyCreateShape('任务1耗时20小时',CellRect(iCol,iRow).Left,GanttGrid.RowHeights[1]*iRow+6+iRow,155,12);
// MyCreateShape('任务2耗时16小时',CellRect(iCol,iRow).Left,RowHeights[1]*iRow+6+iRow,124,12);
end;
//----------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
ClearDynamicComponet(GanttGrid);
end;
//----------------------------------------------------------------------
end.