H
hellopk
Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个TStringGird里做了个显示类似CHECKBOX
但却无法再显示字符串
请各位大虾帮我看看,怎么样既能显示CHECKBOX又能显示字符串??
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
fcheck,fnocheck:tbitmap;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
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.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
SetChkCols : Array of Boolean;
count : Integer;
begin
count := StringGrid1.Rows[0].Count;
SetLength(SetChkCols,count);
SetChkCols[3]:=TRue;
if not (gdFixed in State) then
with TStringGrid(Sender).Canvas do
begin
brush.Color:=clWindow;
FillRect(Rect);
if SetChkCols[ACol] then
begin
if StringGrid1.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
end;
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
if StringGrid1.Cells[StringGrid1.col,StringGrid1.row]='yes' then
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='no'
else
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='yes';
end;
end.
但却无法再显示字符串
请各位大虾帮我看看,怎么样既能显示CHECKBOX又能显示字符串??
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
fcheck,fnocheck:tbitmap;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
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.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
SetChkCols : Array of Boolean;
count : Integer;
begin
count := StringGrid1.Rows[0].Count;
SetLength(SetChkCols,count);
SetChkCols[3]:=TRue;
if not (gdFixed in State) then
with TStringGrid(Sender).Canvas do
begin
brush.Color:=clWindow;
FillRect(Rect);
if SetChkCols[ACol] then
begin
if StringGrid1.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
end;
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
if StringGrid1.Cells[StringGrid1.col,StringGrid1.row]='yes' then
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='no'
else
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='yes';
end;
end.