http://topic.csdn.net/t/20021016/19/1102674.html
这个帖子是CB下的。我在Delphi中改了不行。请高手指教。我不想用第三方控件。
代码如下。
procedure TForm1.FormCreate(Sender: TObject);
var
  mRect:TRect;
  CheckBox1:TCheckBox;
begin
      CheckBox1:=TCheckBox.Create(Form1);
      mRect:=StringGrid1.CellRect(1,1);
      CheckBox1.Parent:=StringGrid1;{CB中这个改成this就行了,可Delphi一改就错!!!}
      CheckBox1.Top:=mRect.Top;
      CheckBox1.Left:=mRect.Left;
      CheckBox1.Width:=mRect.Right-mRect.Left;
      CheckBox1.Height:=mRect.Bottom-mRect.Top;
      CheckBox1.Checked:=true;
end;

解决方案 »

  1.   

    procedure TForm1.FormCreate(Sender: TObject);
    var
      mRect: TRect;
      CheckBox1: TCheckBox;
    begin
      CheckBox1 := TCheckBox.Create(Self);
      mRect := StringGrid1.CellRect(1, 1);
      CheckBox1.Parent := StringGrid1;
      CheckBox1.Top := mRect.Top;
      CheckBox1.Left := mRect.Left;
      CheckBox1.Width := mRect.Right - mRect.Left;
      CheckBox1.Height := mRect.Bottom - mRect.Top;
      CheckBox1.Checked := true;
      CheckBox1.State := cbUnchecked;
    end;
    没有问题。
      

  2.   

    CheckBox1不应该定义的局部的,
      

  3.   

    From 超级猛料  如何编写使StringGrid中的一列具有Check功能,和CheckBox效果一样    
        
    unit Unit1;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Grids;typeTForm1 = class(TForm)grid: TStringGrid;procedure FormCreate(Sender: TObject);procedure gridDrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);procedure gridClick(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;fcheck,fnocheck:tbitmap;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);vari:SmallInt;bmp:TBitmap;beginFCheck:= TBitmap.Create;FNoCheck:= TBitmap.Create;bmp:= TBitmap.create;trybmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));With FNoCheck Do Beginwidth := bmp.width div 4;height := bmp.height div 3;canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );End;With FCheck Do Beginwidth := bmp.width div 4;height := bmp.height div 3;canvas.copyrect(canvas.cliprect,bmp.canvas,rect( width, 0, 2*width, height ));End;finallybmp.freeend;end;procedure TForm1.gridDrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);beginif not (gdFixed in State) thenwith TStringGrid(Sender).Canvas dobeginbrush.Color:=clWindow;FillRect(Rect);if Grid.Cells[ACol,ARow]='yes' thenDraw( (rect.right + rect.left - FCheck.width) div 2,(rect.bottom + rect.top - FCheck.height) div 2,FCheck )elseDraw( (rect.right + rect.left - FCheck.width) div 2,(rect.bottom + rect.top - FCheck.height) div 2,FNoCheck );end;end;procedure TForm1.gridClick(Sender: TObject);beginif grid.Cells[grid.col,grid.row]='yes' thengrid.Cells[grid.col,grid.row]:='no'elsegrid.Cells[grid.col,grid.row]:='yes';end;end.
      

  4.   

     一楼的是可以的。你可以测试一下。CB的THIS=DELPHI的SELF
      

  5.   

    各位,不知道是不是我的意思没有表达清楚。我可以把CheckBox放道Grid的Cell[1,1]中,可是,用鼠标点CheckBox却不响应鼠标事件。二楼的把它设置成UnCheck,运行后,用鼠标点击,却不能变成Check。还有,如果把CheckBox1.Parent:=StringGrid1;中的StringGrid1改成Self,那么,CheckBox就不在StringGrid中了。再就是谢谢楼上贴了用bmp画一个CheckBox的哥们,我在网上查过,但是,既然有CheckBox,我就不想用图片来搞了。现在正在郁闷当中。哪位大侠指点一下。谢谢了。
      

  6.   

    CheckBox1.Parent := Self;
    不要设置成 StringGrid,它可能会提前截获鼠标、键盘事件,导致 CheckBox 不响应。