关于TStringGird 的OnDrawCell 事件的一个问题(100分)

  • 主题发起人 主题发起人 hellopk
  • 开始时间 开始时间
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.

 
加了几行代码,你看看。应该对你有帮助,这时应该在点击后实现显示字符串,
我相信你能得到启发的。

with TStringGrid(Sender).Canvas do
begin
brush.Color:=clWindow;
FillRect(Rect);
if SetChkCols[ACol] then
begin
if StringGrid1.Cells[ACol,ARow]='yes' then
begin
Draw(rect.Left +2,//(rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FCheck );
> rect.Left := rect.Left + FCheck.Width +2;
> DrawText((Sender as TStringGrid).Canvas.Handle,
> Pchar((Sender as TStringGrid).Cells[Acol, ARow]),-1,
> Rect, Dt_VCENTER + DT_LEFT + DT_SINGLELINE);
end

else
begin
Draw( (rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FNoCheck );
> rect.Left := rect.Left + FCheck.Width +2;
> DrawText((Sender as TStringGrid).Canvas.Handle,
> Pchar((Sender as TStringGrid).Cells[Acol, ARow]),-1,
> Rect, Dt_VCENTER + DT_LEFT + DT_SINGLELINE);
end;
end;
end
 
Sorry,忘了把倒数第十行的坐标改了,应该为
Draw(rect.Left +2,// (rect.right + rect.left - FCheck.width) div 2,
 
多人接受答案了。
 
后退
顶部