请StringGrid高手赐教(没多少分了,都拿出来了)(82分)

P

putaopi

Unregistered / Unconfirmed
GUEST, unregistred user!
如何能再Option.goRowSelect:=false时做到,当鼠标选取某一Cell时,被选Cell所在行的所有Cell都变成被选中的颜色。
 
这可是个麻烦的问题
 
以前没用过StringGrid,对他工作原理不太清楚,哪位高手帮帮忙啊
 
可以在click事件中写代码,先判断点击该cell的坐标,然后就知道该行是哪一行了
 
我这样做过,但无论怎忙修改画刷颜色,最后画的时候画刷油变成StringGrid.Color的颜色了
结果.....
 
为什么不设
Option.goRowSelect:=true
这样有必要吗?
 
在 OnDrawCell 中处理!!!!!!
这个效果完全能做到!
你把相关代码贴出来。并指出问题所在。
 
我需要做到用第三方Scrollbar(不能用StringGrid自带的Scrollbar)控制一个StringGrid除前数列外的列向后滚动,
也就是说无论Scrollbar怎样动作,StringGrid的前几列都是不动的,如果Option.goRowSelect:=true;的话就无法取得StringGrid.Seletion.Row的值了。
也就无法设置Scrollbar.position的值了。

onDrawCell事件代码
var
temp : Rect;
begin
temp.top := Rect.top-1;
temp.left := Rect.Left-1;
temp.right := Rect.right+1;
temp.Bottom := Rect.Bottom+1;

StringGrid := TAdvStringGrid(Sender);
StringGrid.Canvas.Pen.color := clRed;
if ARow = ARow then
StringGrid.Canvas.Brush.color := clNavy
else
StringGrid.Canvas.Brush.color := clBlack;
StringGrid.Canvas.Pen.width := 1;

StringGrid.canvas.Rectangle (temp);
end;

但选择某一Cell后只有那一个Cell的颜色变了
 
sorry代码有几行写错了,应该是:
onDrawCell事件代码
var
temp : Rect;
begin
temp.top := Rect.top-1;
temp.left := Rect.Left-1;
temp.right := Rect.right+1;
temp.Bottom := Rect.Bottom+1;

StringGrid := TStringGrid(Sender);
StringGrid.Canvas.Pen.color := clRed;
if ARow = StringGrid.Selection.Row then
StringGrid.Canvas.Brush.color := clNavy
else
StringGrid.Canvas.Brush.color := clBlack;
StringGrid.Canvas.Pen.width := 1;

StringGrid.canvas.Rectangle (temp);
end;
 
implementation

{$R *.DFM}


// 声明以下全局变量
var oldRow:integer;



//这是 STringGrid 的 OnDrawCell 事件处理
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
temp : TRect;
Sg:TStringGrid;
begin
temp.top := Rect.top-1;
temp.left := Rect.Left-1;
temp.right := Rect.right+1;
temp.Bottom := Rect.Bottom+1;

SG:= TStringGrid(Sender);
SG.Canvas.Pen.color := clRed;
if ARow = Sg.Row then
sg.Canvas.Brush.color := clNavy
else
sg.Canvas.Brush.color := clwhite;
sg.Canvas.Pen.width := 1;

sg.canvas.Rectangle (temp);
sg.canvas.Font.color:=$00ff00;
sg.canvas.TextOut(Rect.left+2,Rect.Top+2,sg.cells[ACol,ARow]);
end;

//这是 Form 的 OnCreate 事件处理
procedure TForm1.FormCreate(Sender: TObject);
begin
oldRow:=StringGrid1.Row;
StringGrid1.cells[1,1]:='AAAAA'; //测试
end;


//要处理一下 StringGrid 的 OnSelectCell事件,
//初看没有意义,但是必要的。
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
sg:TStringGrid;
i:integer;
begin
if ARow=oldRow then exit;
sg:=TStringGrid(Sender);
for i:=0 to sg.colCount-1 do
begin
sg.cells[i,oldRow]:=sg.cells[i,oldRow];
sg.cells[i, ARow]:=sg.cells[i, ARow];
end;
oldRow:=ARow;
end;

end.
 
为什么这样就可以了
 
谁能说一下StringGrid的工作原理吗
 
接受答案了.
 
顶部