请问一下 delphi stringgrid如何实现鼠标滑过时给某一行添加颜色?
2个回答
展开全部
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
row,col,maxcol,i,j:integer;
r,oldr:Trect;
begin
stringgrid1.MouseToCell(x,y,col,row); //取得冲纤鼠标指向哪个cell
if (col = oldcol) and (row = oldrow) then Exit;
maxcol := stringgrid1.ColCount-1;
with StringGrid1.Canvas do
begin
for i := 0 to maxcol do //当前cell所在行变色
begin
Brush.Color:=clRed ;
r:= stringgrid1.CellRect(i,row);
FillRect(r);
DrawText(Handle,PAnsiChar(StringGrid1.Cols[i][row]),
-1,r,DT_CENTER or DT_WORD_ELLIPSIS);
end;
sleep(100) ;
if (oldcol=-1) and (oldrow=-1) then
begin
oldcol := col;
oldrow := row;
exit;
end;
for j := 0 to maxcol do //恢复上次改变好伏的颜色
begin
Brush.Color:=clWhite;
oldr:= stringgrid1.CellRect(j,oldrow);
FillRect(oldr);
DrawText(Handle,PAnsiChar(StringGrid1.Cols[j][oldrow]),
-1,oldr,DT_CENTER or DT_WORD_ELLIPSIS);
end;
end;
oldcol := col;
oldrow := row;
end;
其中oldcol和oldrow是定义的变量,初友判携始值-1;
Y: Integer);
var
row,col,maxcol,i,j:integer;
r,oldr:Trect;
begin
stringgrid1.MouseToCell(x,y,col,row); //取得冲纤鼠标指向哪个cell
if (col = oldcol) and (row = oldrow) then Exit;
maxcol := stringgrid1.ColCount-1;
with StringGrid1.Canvas do
begin
for i := 0 to maxcol do //当前cell所在行变色
begin
Brush.Color:=clRed ;
r:= stringgrid1.CellRect(i,row);
FillRect(r);
DrawText(Handle,PAnsiChar(StringGrid1.Cols[i][row]),
-1,r,DT_CENTER or DT_WORD_ELLIPSIS);
end;
sleep(100) ;
if (oldcol=-1) and (oldrow=-1) then
begin
oldcol := col;
oldrow := row;
exit;
end;
for j := 0 to maxcol do //恢复上次改变好伏的颜色
begin
Brush.Color:=clWhite;
oldr:= stringgrid1.CellRect(j,oldrow);
FillRect(oldr);
DrawText(Handle,PAnsiChar(StringGrid1.Cols[j][oldrow]),
-1,oldr,DT_CENTER or DT_WORD_ELLIPSIS);
end;
end;
oldcol := col;
oldrow := row;
end;
其中oldcol和oldrow是定义的变量,初友判携始值-1;
更多追问追答
追问
我用的是2010,DrawText(Handle,PAnsiChar(Grid.Cols[j][oldrow]),这里报错,没有cols这个属性额
追答
我的也是2010哟,没得这个问题哟,测试通过,确实能实现你要的效果
property Cols[Index: Integer]: TStrings read GetCols write SetCols;
这个属性存在的哟
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, FileCtrl, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
Column, Row: Longint;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
i: integer;
begin
StringGrid1.MouseToCell(X, Y, Column, Row);
stringgrid1.Invalidate;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (column>-1) and (Row>-1) and (ARow=Row) then
begin
StringGrid1.Canvas.Brush.Color := clred;
stringgrid1.Canvas.FillRect(Rect);
end;
end;
end.
//dfm文件
object Form1: TForm1
Left = -2
Top = 103
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 36
Top = 52
Width = 529
Height = 321
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
OnMouseMove = StringGrid1MouseMove
end
end
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, FileCtrl, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
Column, Row: Longint;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var
i: integer;
begin
StringGrid1.MouseToCell(X, Y, Column, Row);
stringgrid1.Invalidate;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (column>-1) and (Row>-1) and (ARow=Row) then
begin
StringGrid1.Canvas.Brush.Color := clred;
stringgrid1.Canvas.FillRect(Rect);
end;
end;
end.
//dfm文件
object Form1: TForm1
Left = -2
Top = 103
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 36
Top = 52
Width = 529
Height = 321
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
OnMouseMove = StringGrid1MouseMove
end
end
追问
为什么我加完会闪屏呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询