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;
这个帖子是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;
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;
没有问题。
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.
不要设置成 StringGrid,它可能会提前截获鼠标、键盘事件,导致 CheckBox 不响应。