在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.

解决方案 »

  1.   

    用AdvStringGrid试试看,不用编程可以实现表格按钮.
      

  2.   

    原因好像是因为其无法成为容器。我以前碰到过类似的问题,
    想在LISTVIEW中放个BUTTON,结果拖动滚动条时也是这个问题,解决办法参考:
    1 捕捉滚动条消息,滚动时设置BUTTON位置,具体请参考相关API,不难的
    2 使用TIMER,不断地跟踪BUTTON的位置
    3 去掉BUTTON,改为双击列表时触发。
    我当时解决办法是选择了3,^_^
      

  3.   

    Button是不能去掉的
    项目需要
    有没有别的解决途径呢
      

  4.   

    帮你UP。如果有跟数据库连接的,我一般用EhLib中的DBGridEh.
      

  5.   

    1 捕捉滚动条消息,滚动时设置BUTTON位置,具体请参考相关API,不难的
      

  6.   

    procedure TForm4.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      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;