在StringGrid控件的单元格中动态插入CheckBox?

董董 2001-10-08 04:32:35
请说明编程思路,有现存的例子代码当然更好。谢谢!
...全文
219 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cooling 2002-04-13
  • 打赏
  • 举报
回复
//***********************************

使StringGrid中的一列具有Check功能,和CheckBox效果一样

//***********************************

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;

type
TForm1 = 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;

var
Form1: TForm1;
fcheck,fnocheck:tbitmap;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i:SmallInt;
bmp:TBitmap;
begin
FCheck:= TBitmap.Create;
FNoCheck:= TBitmap.Create;
bmp:= TBitmap.create;
try
bmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));
With FNoCheck Do Begin
width := bmp.width div 4;
height := bmp.height div 3;
canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );
End;
With FCheck Do Begin
width := bmp.width div 4;
height := bmp.height div 3;
canvas.copyrect(
canvas.cliprect,
bmp.canvas,
rect( width, 0, 2*width, height ));
End;
finally
bmp.free
end;
end;
procedure TForm1.gridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if not (gdFixed in State) then
with TStringGrid(Sender).Canvas do
begin
brush.Color:=clWindow;
FillRect(Rect);
if Grid.Cells[ACol,ARow]='yes' then
Draw( (rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FCheck )
else
Draw( (rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FNoCheck );
end;
end;

procedure TForm1.gridClick(Sender: TObject);
begin
if grid.Cells[grid.col,grid.row]='yes' then
grid.Cells[grid.col,grid.row]:='no'
else
grid.Cells[grid.col,grid.row]:='yes';
end;

end.

yanguozhu 2002-03-12
  • 打赏
  • 举报
回复
参考以下程序:

1.//在StringGrid: SpaceGrid中指定的Col, Row嵌入一个ComboBox: SpacingCombo
var
Rect: TRect;
Pnt: TPoint;
begin
with SpaceGrid do
begin
Rect := CellRect(Col, Row);
with Rect do
begin
Pnt := SpacingCombo.Parent.ScreenToClient((SpaceGrid.ClientToScreen(Point(Left, Top))));
SpacingCombo.SetBounds(Pnt.X, Pnt.Y, Right - Left, Bottom - Top);
SpacingCombo.Show;
end;
end;
dolphi 2002-03-12
  • 打赏
  • 举报
回复
没必要动态插入吧?你加入一个CheckBox。visible = false;
需要用的时候显示出来。不用的时候隐藏就好了。
显示之前获得Cell的Rect。改变一下CheckBox的位置。
ihihonline 2002-03-12
  • 打赏
  • 举报
回复
20
呵呵,好啊

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧