如何动态设置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 @ 2024-11-29 13:42  Thenext  阅读(28)  评论(0编辑  收藏  举报