在StringGrid中动态创建按钮,在StringGrid有滚动条的时候,按钮乱跑!好着急,请各位帮忙看看。下面是我的程序在第一列,第三行创建按钮,如果表格没有滚动条就没事,出现滚动条就不对了。项目验收在即,才发现这个问题,各位帮帮忙了!
先谢过了,下面是程序代码
type
TForm4 = class(TForm)
BitBtn1: TBitBtn;
StringGrid1: TStringGrid;
procedure BitBtn1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
fButton:TButton;
gRow,GCol:Integer;
gFreshFlag:bool; public
{ Public declarations }
end;var
Form4: TForm4;implementation{$R *.dfm}procedure TForm4.BitBtn1Click(Sender: TObject);
begin
fButton:=TButton.Create(StringGrid1);
fButton.Parent:=StringGrid1;
fButton.Caption:='aaa';
fButton.Width:=StringGrid1.ColWidths[1]; GCol:=1;
gRow:=3;
gFreshFlag:=true;
StringGrid1.Refresh;
end;procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
fGrid:TStringGrid;
begin Rect:=StringGrid1.CellRect(GCol,gRow);
if gFreshFlag=true then
begin
fButton.SetBounds(Rect.Left,Rect.Top,Rect.Right-Rect.Left,Rect.Bottom-Rect.Top);
gFreshFlag:=false;
endend;end.
先谢过了,下面是程序代码
type
TForm4 = class(TForm)
BitBtn1: TBitBtn;
StringGrid1: TStringGrid;
procedure BitBtn1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
fButton:TButton;
gRow,GCol:Integer;
gFreshFlag:bool; public
{ Public declarations }
end;var
Form4: TForm4;implementation{$R *.dfm}procedure TForm4.BitBtn1Click(Sender: TObject);
begin
fButton:=TButton.Create(StringGrid1);
fButton.Parent:=StringGrid1;
fButton.Caption:='aaa';
fButton.Width:=StringGrid1.ColWidths[1]; GCol:=1;
gRow:=3;
gFreshFlag:=true;
StringGrid1.Refresh;
end;procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
fGrid:TStringGrid;
begin Rect:=StringGrid1.CellRect(GCol,gRow);
if gFreshFlag=true then
begin
fButton.SetBounds(Rect.Left,Rect.Top,Rect.Right-Rect.Left,Rect.Bottom-Rect.Top);
gFreshFlag:=false;
endend;end.
解决方案 »
- 在delphi 中使用C#写的类库
- delphi中的冒号问题
- 注册表问题:为什么不能创建子项?
- (急,求解决)合并数个Access数据库,Insert重复记录时,查询not found,Insert则报错,奇怪问题
- 小问题,请进......
- 如何在某字符串后加上适当长度的空格?
- 我的问题是:你们好啊!如何我把listbox中的每条记录 在我创建的sql数据库中查找到 并输出sql中的和这条记录相关的其他字段到另一个listb
- 加密的数据库的读取?
- 一个简单的问题
- 请教高手:如何实现网络蚂蚁中的代理服务器的设置功能?不够再加.
- ado+odbc+dbf库的清空dbf库的问题。
- delphi 连接 MDB
想在LISTVIEW中放个BUTTON,结果拖动滚动条时也是这个问题,解决办法参考:
1 捕捉滚动条消息,滚动时设置BUTTON位置,具体请参考相关API,不难的
2 使用TIMER,不断地跟踪BUTTON的位置
3 去掉BUTTON,改为双击列表时触发。
我当时解决办法是选择了3,^_^
项目需要
有没有别的解决途径呢
Rect: TRect; State: TGridDrawState);
var
fGrid:TStringGrid;
begin
if State = [gdSelected, gdFocused] then
begin
Rect:=StringGrid1.CellRect(GCol,gRow);
if gFreshFlag=true then
begin
fButton.SetBounds(Rect.Left,Rect.Top,Rect.Right-Rect.Left,Rect.Bottom-Rect.Top);
gFreshFlag:=false;
end
end;