StringGrid 选中某个 单元格时,整行 背景换色。

okmnji79513 2009-03-19 02:20:55
要求:StringGrid 选中 行A中单元格时,行A 背景换色,再选中行B中的单元格时,行B背景换色,行A背景色还原。



搞半天,搞不出来,请指教。
...全文
1107 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nbzip 2009-03-25
  • 打赏
  • 举报
回复
StringGrid1DrawCell这个事件是一直在翻滚的.最好用

if state=select then //这句写错了,只是意思这样
begin
画格内色彩
end
else
begin
如果不选择就画另一种色彩
end


//你不能只考虑一种处理方式,因为他是一直在执行的.就是说,你要处理他全部可能出现的结果.
===================================================关于你问我的问题.
在STRINGGRID中,内嵌一个下拉框,因为上翻下翻的问题,一直到下拉框都超界,越出了字符表格框中.呵呵,我也没处理好.
我上次字符表格框内内嵌字符表格框,也出现这问题.因为任务的原因,后来,改变了一下显示方式.

我感觉.我们所说的内嵌,只是画在当前客户框中,并没有实际关联到格子内部,形成整体. (暂时没这需求,所以放任一下.呵呵,
如果大家有办法,请告知一下. 个人觉得,这种方式,在很多控件中都实现了,应该处理不是太难(我的意思是看人家的源码找灵感).)

----现在我以做任务为主,有空看一下CSDN,要不就是接触或学习别的.对BCB的深入,兴趣不是太大.
okmnji79513 2009-03-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 faith10099 的回复:]
使用自画着色(DefaultDrawing=false):
在OnDrawCell事件中处理你想得到的颜色
[/Quote]


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
R: TRect;
i:integer;
begin
if ARow<>TRect(StringGrid1.Selection).Top then
exit;
for i:=1 to (Sender as TStringGrid).ColCount-1 do
begin
R:=(Sender as TStringGrid).CellRect(i,ARow);

(Sender as TStringGrid).Canvas.Brush.Color:=clGray;
(Sender as TStringGrid).Canvas.FillRect(Rect);
(Sender as TStringGrid).Canvas.Font.Color:=clBlack;
(Sender as TStringGrid).Canvas.TextOut(r.Left+2,r.Top+2,(Sender as TStringGrid).Cells[i,ARow]);
end;
end;

初始状态,第一行 背景都为Gray;点击另一行,只有一个单元格背景变了,该行其他单元格背景不变。WHY?
faith10099 2009-03-19
  • 打赏
  • 举报
回复
使用自画着色(DefaultDrawing=false):
在OnDrawCell事件中处理你想得到的颜色
okmnji79513 2009-03-19
  • 打赏
  • 举报
回复
我看了下nbzip的代码

在Delphi中写下:

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var Rect:TRect;
i:integer;
begin
(Sender as TStringGrid).Refresh;

for i:=1 to (Sender as TStringGrid).ColCount-1 do
begin
Rect:=(Sender as TStringGrid).CellRect(i,ARow);

(Sender as TStringGrid).Canvas.Brush.Color:=clGray;
(Sender as TStringGrid).Canvas.FillRect(Rect);
(Sender as TStringGrid).Canvas.Font.Color:=clBlack;
(Sender as TStringGrid).Canvas.TextOut(rect.Left+2,rect.Top+2,(Sender as TStringGrid).Cells[i,ARow]);
end;
end;

我这个有两个问题:
1、在StringGrid中数据条数比较多时(即 有下拉框),选择某条数据后,滚轮下翻数据--->没问题;滚轮下翻直至 头几条数据 跑出StringGrid外,此时滚轮上翻,当选中单元格一直往上跑,你会看到 Cells[0,0]里会有数据。
2、选中单元格所在行,最多会有2个单元格是 白格。(当你选中 某行某一单元格时,改单元格为白的 其他为Gray,你再点该行另一单元格时,就会有2个 单元格是 白的,其他为Gray)
nbzip 2009-03-19
  • 打赏
  • 举报
回复
噢,你的功能只要sg->Refresh(); 之后就行了.刷新后,原来是什么色彩就是什么色彩.你只需要关注你指定一行要的色彩就行.

nbzip 2009-03-19
  • 打赏
  • 举报
回复
用StringGridSelectCell事件.
你点一行,选一种色彩. 也可以,点一格,选种相关的二三行.下面就是平时的源码.
//============================
void __fastcall Tfm_check_box::sgSelectCell(TObject *Sender, int ACol,
int ARow, bool &CanSelect)
{
// if(sg->RowCount==ARow+1) { CanSelect=false; return; } //最后一行不整行显示
AnsiString str[2];
str[0]=sg->Cells[0][ARow].Trim();
str[1]=sg->Cells[1][ARow].Trim();
if((str[0]==""&&str[1]=="")||str[0]=="*") //
{
if(ACol==2||ACol==3||ACol==4||ACol==5||ACol==6||ACol==7)
sg->Options=sg->Options<<goEditing<<goAlwaysShowEditor<<goTabs;
else sg->Options=sg->Options>>goAlwaysShowEditor>>goEditing>>goTabs;
}else sg->Options=sg->Options>>goAlwaysShowEditor>>goEditing>>goTabs;
//=========================
sg->Refresh();
TRect irect;
for(int i=2;i<sg->ColCount;i++)
{
irect=sg->CellRect(i,ARow);
if((str[0]==""&&str[1]=="")||str[0]=="*")
{
if(i==2||i==3||i==4||i==5||i==6||i==7) sg->Canvas->Brush->Color=0x0012266B;
else sg->Canvas->Brush->Color=0x00C56A31; // 0x00804000
}else sg->Canvas->Brush->Color=0x00C56A31;
sg->Canvas->FillRect(irect);
sg->Canvas->Font->Color=clWhite;
sg->Canvas->TextRect(irect,irect.Left+1,irect.Top+1,sg->Cells[i][ARow]);
} //end for
}
de410 2009-03-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 okmnji79513 的回复:]
引用 1 楼 de410 的回复:
直接将StringGrid option属性中的gorowselect设为true即可~~

还是慢了点...... -.-|||
[/Quote]

将StringGrid option属性中的godrawfocusselected设为true即可~~但不是一行,而是单元格,这样不会那么麻烦写那么多代码~~
okmnji79513 2009-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 de410 的回复:]
直接将StringGrid option属性中的gorowselect设为true即可~~
[/Quote]
还是慢了点...... -.-|||
okmnji79513 2009-03-19
  • 打赏
  • 举报
回复
注意是goEditing=true、goRowSelect=false,如果goRowSelect=true的话就没法编辑了。
de410 2009-03-19
  • 打赏
  • 举报
回复
直接将StringGrid option属性中的gorowselect设为true即可~~

5,507

社区成员

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

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