如何动态设置richedit的字体和背景颜色(原创)

因为要使用richedit 显示不同颜色的日志所以要动态的设置新插入行的颜色

 

复制代码
 1 frmServerMain.redtLog.Lines.BeginUpdate;
 2 // 选择文本所在的位置
 3 
 4 //首先要清除选择行
 5 
 6 1 redtLog.SelStart := frmServerMain.redtLog.GetTextLen;
 7 2 RedtLog.SelLength := 0; // 清空选择
 8 //然后根据颜色设置新添加行的颜色
 9 
10 case aColor of
11 rared:
12 begin
13 redtLog.SelStart := redtLog.GetTextLen - Length(AddLogStr)
14 - (redtLog.Lines.Count + 1);
15 redtLog.SelLength := Length(AddLogStr);
16 redtLog.SelAttributes.Color := clRed;
17 end;
18 raGreen:
19 begin
20 redtLog.SelStart := redtLog.GetTextLen - Length(AddLogStr)
21 - (redtLog.Lines.Count + 1);
22 redtLog.SelLength := Length(AddLogStr);
23 redtLog.SelAttributes.Color := clGreen;
24 end;
25 
26 raskyblue:
27 begin
28 redtLog.SelStart :=redtLog.GetTextLen - Length(AddLogStr)
29 - (redtLog.Lines.Count + 1);
30 redtLog.SelLength := Length(AddLogStr);
31 redtLog.SelAttributes.Color := clBlue;
32 end;
33 
34 rawindows:
35 begin
36 redtLog.SelStart := redtLog.GetTextLen - Length(AddLogStr)
37 - (redtLog.Lines.Count + 1);
38 redtLog.SelLength := Length(AddLogStr);
39 redtLog.SelAttributes.Color := clBlack;
40 end;
41 end;
42 finally
43  redtLog.Lines.EndUpdate;
44 end;
复制代码

 

 
posted @   Thenext  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-11-29 雪花算法(SNOWFLAKE)DELPHI版
点击右上角即可分享
微信分享提示