想要的效果是这样的:
1。在一个单元格中可以同时存在多种颜色。
2.能控制(显示或隐藏)这个单元格中字符串的某一部份字符串。
3.能自动换行显示。或者说:现在需要将一个字符串中的特别字符用特定的颜色来显示,不管是用stringgrid,或者是 edit 或者是 label 都可以。
注:不要用多个label来实现。如:字符串为: 黑色 绿色 红色
要显示为: 黑色这两个字符用 黑色显示,绿色这两个字符用绿色显示,红色这两个字符用红色显示
需要注意的一点是 “黑色 绿色 红色”是一个完整的字符串。昨天搞到了2点也没有想出好办法,请大家指点一下。分不成问题。
这和语法分析有点像,也下载了一个语法分析的组件,目前还没有看懂。
但是这样做对自动换行有影响,总觉得这种思想不太对。特来发帖。
谢谢你的关注。
说的有道理。我在试。
我正有此意。
像这样的功能在html里是很容易。
一个TLabel 中可以有多种字体同时存在,我想不管是什么组件,最终的方法应该是相差不大的。
Rect: TRect; State: TGridDrawState);
begin StringGrid1.Canvas.Font.Color := clred;
StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,'红色');
StringGrid1.Canvas.Font.Color := clGreen;
StringGrid1.Canvas.TextOut(Rect.Left+ length('红色' )*Font.Size ,Rect.Top,'绿色');
StringGrid1.Canvas.Font.Color := clBlack;
StringGrid1.Canvas.TextOut(Rect.Left+ length('红色绿色' )*Font.Size ,Rect.Top,'黑色');
end;
其中的字符前可以在画之前处理好,以及每个部分的长度。我这里就简单的用length了。
这样可以简单处理一下字的颜色,不过字间距可能要重新处理一下,要换算成象素。换行呢就是把字符长度和单元格宽度比较一下,这个应该不难。
StringGrid1.Canvas.Font.Name := '宋体';
StringGrid1.Canvas.TextOut(Rect.Left,Rect.Top,'红色');
StringGrid1.Canvas.Font.Color := clGreen ;
StringGrid1.Canvas.Font.Name := '隶书';
StringGrid1.Canvas.TextOut(Rect.Left+ StringGrid1.Canvas.TextWidth('红色') ,Rect.Top,'绿色');
StringGrid1.Canvas.Font.Color := clBlack;
StringGrid1.Canvas.Font.Name := '楷体_GB2312';
StringGrid1.Canvas.TextOut(Rect.Left+ StringGrid1.Canvas.TextWidth('红色绿色') ,Rect.Top,'黑色');
感你的代码。thanks.
我分少了,来挣点分
//原因是空格换行的时候会影响宽度计算
//处理换行输出的时候改成每一行分别输出就可以了
//具体你的情况是什么还不清楚,贴出代码供参考type
TTextColor = record
rText: string;
rColor: TColor;
end;const
cTextColors: array[0..4] of TTextColor =
(
(rText: '黑色'; rColor: clBlack),
(rText: '绿色'; rColor: clGreen),
(rText: '红色'; rColor: clRed),
(rText: '黄色'; rColor: clYellow),
(rText: '蓝色'; rColor: clBlue)
);procedure TForm1.FormCreate(Sender: TObject);
begin
Font.Name := '宋体';
Font.Size := 9;
StringGrid1.Cells[1, 1] := '黑色 绿色 红色';
StringGrid1.Cells[1, 2] := '绿色 绿色 黄色';
StringGrid1.Cells[1, 3] := '蓝色 绿色 红色';
StringGrid1.Cells[1, 4] := '绿色 黄色 蓝色';
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
S: string;
T: string;
vText: string;
vBrushStyle: TBrushStyle;
I, J: Integer;
begin
with TStringGrid(Sender) do try
Canvas.Lock;
Canvas.FillRect(Rect);
Canvas.Font.Assign(Self.Font);
Canvas.Font.Color := clDefault;
S := Cells[ACol, ARow];
{ TODO : 隐藏S中的部分,这比较简单不举例 }
DrawText(Canvas.Handle, PChar(S), Length(S), Rect,
DT_EDITCONTROL or DT_WORDBREAK);
vBrushStyle := Canvas.Brush.Style;
Canvas.Brush.Style := bsClear;
for I := Low(cTextColors) to High(cTextColors) do
begin
T := S;
J := Pos(cTextColors[I].rText, T);
Canvas.Font.Color := cTextColors[I].rColor;
while J > 0 do
begin
vText := StringOfChar(#32, J - 1) + cTextColors[I].rText;
FillChar(T[1], J + Length(cTextColors[I].rText), #0);
Windows.DrawText(Canvas.Handle, PChar(vText), Length(vText),
Rect, DT_EDITCONTROL or DT_WORDBREAK);
J := Pos(cTextColors[I].rText, T);
end;
end;
Canvas.Brush.Style := vBrushStyle;
Canvas.Font.Assign(Self.Font);
finally
Canvas.Unlock;
end;
end;
本回复由大傻的破玩意儿【CSDN's forum Explorer】完成!
大傻的破玩意儿,今天你用了木有 !!
软件功能强大,速度超快!!支持中...
软件下载地址:http://CoolSlob.ys168.com