1. 程式人生 > >在StringGrid中每行新增一個選擇框(checkbox)

在StringGrid中每行新增一個選擇框(checkbox)

    StringGrid得實際使用中,經常會遇到這樣的需求,使用者想對每一行的記錄作一個標記,想對做了標記的記錄作特別的操作。例如想刪除多條記錄。

    在網頁中經常有類似的應用,例如在電子郵箱裡,顯示郵件的每一行都有一個選擇框,使用者可以對多個郵件同時做刪除,移動的操作。
    在delphi的StringGrid中雖然沒有直接提供在每一行插入一個checkbox的功能,可是我們可以通過編寫程式來給它加上一個checkbox,讓使用者對記錄進行選擇。

通過在網路上搜索一些相關資料 我寫了一小程式,驗證通過。
該程式就實現了在每一行都顯示一個checkbox,然後可以對每一行的記錄進行選擇。

關鍵詞:StringGrid,嵌入控制元件,canvas,Loadbitmap;

unit SRGrid;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, Grids, StdCtrls, DBCtrls, DBGrids, DB, ADODB;

type
   TForm1 = class(TForm)
     conn: TADOConnection;
     qry: TADOQuery;
     strGrid: TStringGrid;
     ctnClear: TButton;
     btnSearch: TButton;
     edtDW: TEdit;
     btnBLItem: TButton;
     procedure FormCreate(Sender: TObject);
     procedure strGridDrawCell(Sender: TObject; ACol, ARow: Integer;
       Rect: TRect; State: TGridDrawState);
     procedure btnSearchClick(Sender: TObject);
     procedure strGridMouseDown(Sender: TObject; Button: TMouseButton;
       Shift: TShiftState; X, Y: Integer);
     procedure ctnClearClick(Sender: TObject);
     procedure btnBLItemClick(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
     DeleteObject(bmp.Handle);
     bmp.Free;
   end;
end;

procedure TForm1.strGridDrawCell(Sender: TObject; ACol, ARow: Integer;
   Rect: TRect; State: TGridDrawState);
begin
if Acol=0 then
begin
   if not(gdFixed in State) then
     with Tstringgrid(Sender).Canvas do
     begin
       brush.Color:=clWindow;
       FillRect(Rect);
       if strGrid.Cells[ACol,ARow]='yes' then
         Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FCheck );
       if strGrid.Cells[ACol,ARow]='no' then
         Draw( (rect.right + rect.left - FCheck.width) div 2, (rect.bottom + rect.top - FCheck.height) div 2, FNoCheck );
     end;
   end;
end;

procedure TForm1.btnSearchClick(Sender: TObject);
var
   i,j:integer;
   strsql:string;
   strwhere:string;
begin
   strwhere:='';
   if edtDW.Text<>'' then
     strwhere:=' where request_corp like ''%'+edtDW.Text+'%''';
   with qry do
   begin
     close;
     sql.Clear;
     strsql:='select ITEM_ID,STATUS,REQUEST_CORP,ADDR,REQUEST_TIME from xk_t_item ';
     strsql:=strsql+strwhere;
     sql.Add(strsql);
     open;
     strGrid.RowCount:=recordcount+1;
     strGrid.Update;
     i:=1;
     while not eof do
     begin
       for j:=1 to strGrid.ColCount-2 do
       begin
         if j=1 then strGrid.Cells[j-1,i]:='no';
         strGrid.Cells[j,i]:=Fields[j-1].AsString;
         //showmessage('cell['+inttostr(i)+','+inttostr(j)+']的值:'+fields[j].AsString);
       end;
       i:=i+1;
       next;
     end;
   end;
end;

procedure TForm1.strGridMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer);
begin
   if not( strGrid.Col=0 )then exit;
   if strGrid.Cells[strGrid.Col,strGrid.Row]='yes' then
     strGrid.Cells[strGrid.col,strGrid.row]:='no'
   else
     strGrid.Cells[strGrid.col,strGrid.row]:='yes';
end;

procedure TForm1.ctnClearClick(Sender: TObject);
var
   strItemid:string;
   nRow:integer;// 當前所在行;
begin
   nRow:=strGrid.Row;
   strItemid:=strGrid.Cells[1,nRow];
   showmessage(strItemid);
end;

procedure TForm1.btnBLItemClick(Sender: TObject);
var
i:integer;
irows:integer;
strFlag:string;
itemList:array of string;
begin
i:=strGrid.RowCount-1;
for i:=1 to irows do
begin
    strFlag:=strGrid.Cells[0,i];
    if strFlag='yes' then

end;
end;

轉子:http://hi.baidu.com/wangkuoguang/item/22da211412189b24f6625ca3