TStringGrid使用(1)[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 cureshy 的 blog

stringgrid行列的增加和删除

type
texcell = class(tstringgrid)

public
procedure deleterow(arow: longint);
procedure deletecolumn(acol: longint);
procedure insertrow(arow: longint);
procedure insertcolumn(acol: longint);
end;

procedure texcell.insertcolumn(acol: integer);
begin
colcount :=colcount +1;
movecolumn(colcount-1, acol);
end;

procedure texcell.insertrow(arow: integer);
begin
rowcount :=rowcount +1;
moverow(rowcount-1, arow);
end;

procedure texcell.deletecolumn(acol: longint);
begin
movecolumn(acol, colcount -1);
colcount := colcount - 1;
end;

procedure texcell.deleterow(arow: longint);
begin
moverow(arow, rowcount - 1);
rowcount := rowcount - 1;
end;



如何编写使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.


stringgrid组件cells内容分行显示在tstringgrid.ondrawcell事件中


drawtext(stringgrid1.canvas.handle,pchar(stringgrid1.cells[acol,arow]),length(stringgrid1.cells[acol,arow]),rect,dt_wordbreak or dt_left);

可以实现文字换行!


在stringgrid怎样制作只读的列在 onselectcell事件处理程序中,加入: (所有的列均设成可修改的)



if col mod 2 = 0 then
  grd.options := grd.options + [goediting]
else
  grd.options := grd.options - [goediting];


stringgrid从文本读入的问题(save/load a tstringgrid to/from a file?)stringgrid从文本读入的问题


// save a tstringgrid to a file
procedure savestringgrid(stringgrid: tstringgrid; const filename: tfilename);
var
f: textfile;
i, k: integer;
begin
assignfile(f, filename);
rewrite(f);
with stringgrid do
begin
  // write number of columns/rows
  writeln(f, colcount);
  writeln(f, rowcount);
  // loop through cells
  for i := 0 to colcount - 1 do
   for k := 0 to rowcount - 1 do
    writeln(f, cells[i, k]);
end;
closefile(f);
end;

// load a tstringgrid from a file
procedure loadstringgrid(stringgrid: tstringgrid; const filename: tfilename);
var
f: textfile;
itmp, i, k: integer;
strtemp: string;
begin
assignfile(f, filename);
reset(f);
with stringgrid do
begin
  // get number of columns
  readln(f, itmp);
  colcount := itmp;
  // get number of rows
  readln(f, itmp);
  rowcount := itmp;
  // loop through cells & fill in values
  for i := 0 to colcount - 1 do
   for k := 0 to rowcount - 1 do
   begin
    readln(f, strtemp);
    cells[i, k] := strtemp;
   end;
  end;
closefile(f);
end;

// save stringgrid1 to 'c:.txt':
procedure tform1.button1click(sender: tobject);
begin
savestringgrid(stringgrid1, 'c:.txt');
end;

// load stringgrid1 from 'c:.txt':
procedure tform1.button2click(sender: tobject);
begin

本文关键:TStringGrid使用(1)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top