在StringGrid中每行添加一个选择框(checkbox)

来源:互联网 发布:php去除重复数组值 编辑:程序博客网 时间:2024/05/18 02:36
    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

原创粉丝点击