StringGrid 同一个单元格 显示 不同颜色的字体

wealsh 2012-07-05 04:53:54
比如: StringGrid.Cells[1,1]='StringGrid 同一个单元格颜色'

把其中的 “单元格” 颜色改为蓝色 如何实现?谢谢
...全文
300 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
广州佬 2012-07-06
  • 打赏
  • 举报
回复
知道了方法,就该懂得灵活处理。

在代码中加上对列宽与字符长度关系的判断,相应设置好显示位置就ok了呀。

wealsh 2012-07-06
  • 打赏
  • 举报
回复
谢谢!

不过还有一种情况,如果我设置为自动换行的话如何处理?

HCell:=DrawText(Canvas.Handle, PChar(SCell), Length(SCell), Rect,DT_WORDBREAK or DT_LEFT);
RowHeights[ARow] := HCell+5;

比如自动换行之后 “单元” 这两个字为蓝色,但 “格” 这个字就没有变化了
StringGrid同一个单元
格颜色
shuihan20e 2012-07-06
  • 打赏
  • 举报
回复
用画布是个不错误的方法,不过,有时会定位不太准确
kaikai_kk 2012-07-06
  • 打赏
  • 举报
回复
1楼不错
就是用Canvas,先设置Font字体颜色,再用TextOut方法在不同的位置输出文字
广州佬 2012-07-06
  • 打赏
  • 举报
回复
你还可以在http://topic.csdn.net/u/20110924/12/2aaaf583-cc5f-4c0e-9056-afe92711420a.html的23楼至25楼看我回复帖子的做法和效果。
广州佬 2012-07-05
  • 打赏
  • 举报
回复
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[1,1]:='StringGrid同一个单元格颜色';
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var s:String;
i,len,w1,w2:integer;
begin
if(ACol=1)and(ARow=1)then begin
s:=StringGrid1.Cells[1,1];
if trim(s)='' then exit;
len:=pos('单元格',s);
w1:=0;
w2:=0;
for i:=1 to length(s) do begin
if i<len then w1:=w1+StringGrid1.Canvas.TextWidth(s[i])
else if i<len+6 then w2:=w2+StringGrid1.Canvas.TextWidth(s[i])
end;
StringGrid1.Canvas.Font.Color:=clWindowText;
StringGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+4,copy(s,1,len-1));
StringGrid1.Canvas.Font.Color:=clBlue;
StringGrid1.Canvas.TextOut(Rect.Left+w1+4,Rect.Top+4,'单元格');
if length(s)>len+5 then begin
StringGrid1.Canvas.Font.Color:=clRed;
StringGrid1.Canvas.TextOut(Rect.Left+w1+w2+8,Rect.Top+4,copy(s,len+6,length(s)));
end;
end;
end;

5,392

社区成员

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

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