目录
一、前言
二、实现方法
1. 第一步
2. 第二步
3. 第三步
三、主程序代码
四、下载
1. 可执行程序
2. 程序源代码
在用Delphi做日常开发的时候,经常需要显示程序运行的日志,一般我们会使用TMemo,使用起来简单,方便。但是缺点也很明显,就是只能显示单色文字,字体也只能统一设置,无法实现个性颜色和字体。
但是我们看大的一些系统,日志则非常美观、漂亮、专业。
例如,Delphi的编译日志如下:
那么,我们能收实现这样的日志显示框呢,当然可以,而且非常简单,无需使用任何第三方控件,使用Delphi自带的TRichEdit就可以。
通过对TRichEdit进行Helper构造,增加一个显示多颜色的命令即可。
编写一个uTRichEdit_Helper.pas单元,在该单元中增加一个Add_Color_Log方法,该方法有5个参数:
序号 | 参数 | 类别 | 默认 | 说明 |
1 | msg | string | 无 | 需要显示的消息字符串 |
2 | FontColor | TColor | clBlack(黑色) | 字体显示的颜色 |
3 | FontSize | Byte | 9 | 字体大小 |
4 | FontStyle | TFontStyles | [ ] | 字体格式 |
5 | LeftMargin | Byte | 4 | 左边预留空格 |
{*******************************************************
* 时间:2023-12-03
* 作者:sensor wu
* 功能:实现TRichEdit的彩色可编程 Log 功能
*
* 此处为 VCL,FMX不支持该控件
*
* 说明:
* msg: 需要显示的消息内容
* FontColor: 需要现实的颜色,默认黑色
* FontSize : 字体大小,默认9号字体
* FontStyle: 字体是否加粗、斜体等显示,默认不是
* LeftMargin: 左边留空格数量,默认是4
*
* 快捷用法:
* 1. RichEdit.Add_Color_log('编译成功!'#13#10, clGreen); //显示一行绿色
* 2. RichEdit.Add_Color_log('编译错误:');
* RichEdit.Add_Color_log('23行缺少逗号...'#13#10,clRed); //显示一行:编译错误: 23行缺少逗号... 前面黑色,后面红色
*******************************************************}
unit uTRichEdit_Helper;
interface
uses
Vcl.Graphics, //TColor
Winapi.Messages, //消息常量,例如:EM_SCROLLCARET ,为了减少空间,可以不引该单元,直接使用数字解开
Winapi.Windows, //消息常量,例如:SB_ENDSCROLL
Vcl.ComCtrls;
type
TRichEditHelper = class helper for TRichEdit
public
procedure Add_Color_log(msg: string; FontColor : TColor = clBlack; FontSize : Byte = 9; FontStyle : TFontStyles = []; LeftMargin: Byte = 4);
end;
implementation
{ TRichEditHelper }
procedure TRichEditHelper.Add_Color_log(msg: string; FontColor: TColor;
FontSize: Byte; FontStyle: TFontStyles; LeftMargin: Byte);
begin
//此处省略了实现代码
end;
end.
在主程序中引用该单元:
implementation
uses
uTRichEdit_Helper;
使用:
RichEdit1.Add_Color_log(#13#10);
RichEdit1.Add_Color_log('Checking project dependencies...'#13#10,clBlack,9,[fsBold]);
RichEdit1.Add_Color_log('Compiling RichEdit_Helper_Demo.dproj (Debug,Win32)'#13#10,clBlack,9,[fsBold]);
RichEdit1.Add_Color_log('brcc32 command line for "RicheEdit_Helper_Demo.vrc"'#13#10,clBlack);
RichEdit1.Add_Color_log('dcc32 command line for "RicheEdit_Helper_Demo.dpr"'#13#10,clBlack);
RichEdit1.Add_Color_log('Success'#13#10,clGreen,9,[fsBold]);
RichEdit1.Add_Color_log('Elapsed time: 00:00:01.2'#13#10);
unit uMainForm_RichEdit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdUDPServer, IdGlobal, IdSocketHandle,
Vcl.ExtCtrls, IdUDPClient, System.ImageList, Vcl.ImgList,
Vcl.VirtualImageList, Vcl.BaseImageCollection, Vcl.ImageCollection,
IdBaseComponent, IdComponent, IdUDPBase, Vcl.Menus, Vcl.ExtActns,
System.Actions, Vcl.ActnList, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.VirtualImage,
Vcl.Buttons;
type
TForm_RichEdit = class(TForm)
Panel_Command: TPanel;
SpeedButton6: TSpeedButton;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton10: TSpeedButton;
VirtualImage1: TVirtualImage;
Panel_Client: TPanel;
Panel_LeftCommand: TPanel;
SpeedButton4: TSpeedButton;
RichEdit1: TRichEdit;
ActionList1: TActionList;
Action_Start: TAction;
Action_Stop: TAction;
Action_RegService: TAction;
Action_UnRegService: TAction;
Action_SetParams: TAction;
SendMail1: TSendMail;
ImageCollection1: TImageCollection;
VirtualImageList1: TVirtualImageList;
procedure SpeedButton4Click(Sender: TObject);
procedure Action_StartExecute(Sender: TObject);
procedure Action_SetParamsExecute(Sender: TObject);
procedure Action_StopExecute(Sender: TObject);
procedure Action_RegServiceExecute(Sender: TObject);
procedure Action_UnRegServiceExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_RichEdit: TForm_RichEdit;
implementation
uses
uTRichEdit_Helper;
{$R *.dfm}
procedure TForm_RichEdit.Action_RegServiceExecute(Sender: TObject);
begin
RichEdit1.Add_Color_log('Web服务端口号: ');
RichEdit1.Add_Color_log('8080'#13#10,clBlue,9,[]);
end;
procedure TForm_RichEdit.Action_SetParamsExecute(Sender: TObject);
begin
RichEdit1.Add_Color_log(#13#10);
RichEdit1.Add_Color_log('Checking project dependencies...'#13#10,clBlack,9,[fsBold]);
RichEdit1.Add_Color_log('Compiling RichEdit_Helper_Demo.dproj (Debug,Win32)'#13#10,clBlack,9,[fsBold]);
RichEdit1.Add_Color_log('brcc32 command line for "RicheEdit_Helper_Demo.vrc"'#13#10,clBlack);
RichEdit1.Add_Color_log('dcc32 command line for "RicheEdit_Helper_Demo.dpr"'#13#10,clBlack);
RichEdit1.Add_Color_log('Success'#13#10,clGreen,9,[fsBold]);
RichEdit1.Add_Color_log('Elapsed time: 00:00:01.2'#13#10);
end;
procedure TForm_RichEdit.Action_StartExecute(Sender: TObject);
begin
RichEdit1.Add_Color_log('服务已经成功启动 (绿色)'#13#10,clGreen,9,[]);
end;
procedure TForm_RichEdit.Action_StopExecute(Sender: TObject);
begin
RichEdit1.Add_Color_log('服务已经成功停止 (红色)'#13#10,clRed,9,[]);
end;
procedure TForm_RichEdit.Action_UnRegServiceExecute(Sender: TObject);
begin
RichEdit1.Add_Color_log('UDP在这个端口');
RichEdit1.Add_Color_log('8192',clGreen,9,[fsBold,fsItalic]);
RichEdit1.Add_Color_log('收到数据');
RichEdit1.Add_Color_log('(' + FormatDateTime('YYYY-MM-DD hh:mm:ss',Now) +')'#13#10,clGray,9,[]);
end;
procedure TForm_RichEdit.SpeedButton4Click(Sender: TObject);
begin
RichEdit1.Clear;
end;
end.
下载(EXE)程序
下载(源程序:¥14.9)