请问如何在stringGrid重实现打勾,也就是说,我想在其某列上实现一个Boolean型的列
类似于checkBox的功能;(非dbgrid)

解决方案 »

  1.   


    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. 
     
       
      

  2.   

    用两个图片:一个打勾,一个不打勾
    将图片画在stringgrid 中
    在onclick事件中切换两个图片即可
      

  3.   

    郁闷个啥在COL(0)里面打勾不就得
      

  4.   

    stringgrid中嵌套一个checkbox控件不就得了,还能得到很多属性和事件呢
    checkbox1.left:=stringgrid1.cellrect[].left;
    checkbox1.top:=stringgrid1.cellrect[].top;
    checkbox1.height:=stringgrid1.cellrect[].height;
    checkbox1.width:=stringgrid1.cellrect[].width;
      

  5.   

    stringgrid中嵌套一个checkbox控件不就得了,还能得到很多属性和事件呢
    checkbox1.left:=stringgrid1.cellrect[].left;
    checkbox1.top:=stringgrid1.cellrect[].top;
    checkbox1.height:=stringgrid1.cellrect[].height;
    checkbox1.width:=stringgrid1.cellrect[].width;再在每次重绘时记得:checkbox1.caption:=stringgrid1.cells[]一下