StringGrid指定单元格背景颜色改变

吴恒 2011-01-02 04:00:17
怎么样让选中的单元格及其它前面的单元格的背颜色改变?如选中单元格的为row = 5,col = 4,让row=5 ,col=3的单元格的颜色也改变?
...全文
1026 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
野草飞雪 2011-01-10
  • 打赏
  • 举报
回复
用画笔即可
tomorrow619810 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]

通过
StringGrid1.CellRect(StringGrid1.Col-1,StringGrid1.Row);获得另一个单元格的Rect,然后画上底色
[/Quote]

Tbrush.color=clred;
fillrect(StringGrid1.CellRect(StringGrid1.Col-1,StringGrid1.Row))
  • 打赏
  • 举报
回复
继承这个类写个新的类,或者把代码放到窗体的刷新事件中
软件钢琴师 2011-01-06
  • 打赏
  • 举报
回复
用advstringgrid 吧
procedure TForm1.FormCreate(Sender: TObject);
begin
AdvStringGrid1.Colors[2,1] := clblue;
end;
很简单
dyf2001 2011-01-06
  • 打赏
  • 举报
回复

procedure TForm1.FormCreate(Sender: TObject);
begin
self.FCol := 1;
self.FRow := 1;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (ACol = 0) or (ARow = 0) then
Exit;
if ((ACol = self.FCol) and (ARow = self.FRow)) or ((ACol = self.FCol-1) and (ARow = self.FRow) and (ACol>0)) then
begin
self.StringGrid1.Canvas.Brush.Color := clRed;

end
else
self.StringGrid1.Canvas.Brush.Color := clwhite;
self.StringGrid1.Canvas.FillRect(Rect);
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
self.FCol := ACol;
self.FRow := ARow;
self.StringGrid1.Refresh;
end;

大概测试了一些可以,细节可以自己再研究
火龙岛主 2011-01-05
  • 打赏
  • 举报
回复
加入Invalidate,刷新一下。
吴恒 2011-01-05
  • 打赏
  • 举报
回复
不起作用,还是刷不过来,选中哪个只会刷新那个单元格的底色。高手给段代码吧。
吴恒 2011-01-02
  • 打赏
  • 举报
回复
这样改不能自动刷新相邻的单元格呀,把界面最小化然后再还原还能看到效果了。
if (state * [gdSelected]) = [gdSelected] then
begin
if acol mod 2 = 0 then
begin
DrawCol(arow,acol,rgb(223,249,221),clblue,[],rect) ;

newRect:=stringgrid1.CellRect(acol+1,arow);
drawcol(arow,acol+1,rgb(223, 249,221),clblack,[],newRect);

end
else
begin
DrawCol(arow,acol,rgb(223,249,221),clblack,[],rect) ;
newRect:=stringgrid1.CellRect(acol-1,arow);
drawcol(arow,acol-1,rgb(223, 249,221),clblue,[],newRect);

end;
end
else
begin

begin

if (arow = 0 )then
DrawCol(arow,acol,clwhite,clFuchsia,[fsbold],rect)
else if (acol mod 2 = 0) then
DrawCol(arow,acol,clwhite,clblue,[],rect)

else
DrawCol(arow,acol,clwhite,clblack,[],rect);
end;
end;


procedure DrawCol(row, col: integer;color,fontcolor:Tcolor;fontstyle:TFontStyles;rect:Trect);
var
nrect:TRect;
str:string;
h,w,lx,ly,x,y:integer;

begin
//rect:=stringgrid1.CellRect(col,row);
stringgrid1.canvas.Brush.Color:=color; // rgb(231,243,226)
stringgrid1.Canvas.FillRect(rect);
stringgrid1.canvas.Font.Name:='宋体';
stringgrid1.canvas.Font.size:=10;
stringgrid1.canvas.Font.style:=fontstyle;
// if col mod 2 = 0 then
stringgrid1.canvas.Font.color:=fontcolor;
// else
// stringgrid1.Canvas.Font.Color:=clblack;
str:=stringgrid1.cells[col,row];
h := stringgrid1.Canvas.TextHeight(str);
w:=stringgrid1.Canvas.TextWidth(str);
lx:=(rect.Right-rect.left - w) div 2;
ly:=(rect.Bottom -rect.Top - h) div 2;
x:=rect.Left+lx;
y:=rect.Top+ly ;
stringgrid1.Canvas.TextOut(x,y, str);

end;
bdmh 2011-01-02
  • 打赏
  • 举报
回复
通过
StringGrid1.CellRect(StringGrid1.Col-1,StringGrid1.Row);获得另一个单元格的Rect,然后画上底色

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧