TRichEdit 具有以下属性:EnableURLs
和ShowURLHint
。EnableURLs 自动将 Internet URL(例如http://www.example.com)转换为超链接。
使用下面的代码,我尝试使用电子邮件地址实现相同的功能。这仅在视觉上有效:
问题:但是,如何在单击电子邮件地址时自动实现[email protected]超链接功能(crHandPoint 光标和 OnURLClick 事件处理程序)?
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Label1: TLabel;
procedure RichEdit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.RegularExpressions;
procedure DetectAndLinkifyEmailsAndURLs(RichEdit: Vcl.ComCtrls.TRichEdit);
var
Match: System.RegularExpressions.TMatch;
EmailPattern, URLPattern: string;
LineIndex, LineStart, StartPos, SelLength: Integer;
LineText: string;
PrevSelStart, PrevSelLength: Integer;
begin
EmailPattern := '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b';
URLPattern := '\b(http|https)://[^\s]+';
// Disable redraw to avoid flicker
SendMessage(RichEdit.Handle, WM_SETREDRAW, WPARAM(False), 0);
try
// Save user selection
PrevSelStart := RichEdit.SelStart;
PrevSelLength := RichEdit.SelLength;
// Reset all formatting to default for the entire text
RichEdit.SelectAll;
RichEdit.SelAttributes.Color := Vcl.Graphics.clWindowText;
RichEdit.SelAttributes.Style := [];
// Process line by line
for LineIndex := 0 to RichEdit.Lines.Count - 1 do
begin
LineText := RichEdit.Lines[LineIndex];
LineStart := RichEdit.Perform(EM_LINEINDEX, LineIndex, 0); // Get line start position
// Format email addresses in the line
Match := System.RegularExpressions.TRegEx.Match(LineText, EmailPattern, [System.RegularExpressions.roIgnoreCase]);
while Match.Success do
begin
StartPos := LineStart + Match.Index - 1;
SelLength := Match.Length;
RichEdit.SelStart := StartPos;
RichEdit.SelLength := SelLength;
RichEdit.SelAttributes.Color := Vcl.Graphics.clBlue;
RichEdit.SelAttributes.Style := [Vcl.Graphics.fsUnderline];
Match := Match.NextMatch;
end;
// Format URLs in the line
Match := System.RegularExpressions.TRegEx.Match(LineText, URLPattern, [System.RegularExpressions.roIgnoreCase]);
while Match.Success do
begin
StartPos := LineStart + Match.Index - 1;
SelLength := Match.Length;
RichEdit.SelStart := StartPos;
RichEdit.SelLength := SelLength;
RichEdit.SelAttributes.Color := Vcl.Graphics.clBlue;
RichEdit.SelAttributes.Style := [Vcl.Graphics.fsUnderline];
Match := Match.NextMatch;
end;
end;
// Restore user selection
RichEdit.SelStart := PrevSelStart;
RichEdit.SelLength := PrevSelLength;
// Ensure the current selection uses default formatting
if (PrevSelStart >= 0) and (PrevSelLength = 0) then
begin
RichEdit.SelAttributes.Color := Vcl.Graphics.clWindowText;
RichEdit.SelAttributes.Style := [];
end;
finally
// Re-enable redraw
SendMessage(RichEdit.Handle, WM_SETREDRAW, WPARAM(True), 0);
RichEdit.Invalidate; // Refresh to reflect changes
end;
end;
procedure TForm1.RichEdit1Click(Sender: TObject);
begin
DetectAndLinkifyEmailsAndURLs(RichEdit1);
end;
end.
自动 URL 检测功能支持电子邮件地址。您只需在其前面加上
mailto:
URL 方案即可。如果您不想这样做,那么在 Windows 8 及更高版本中,您可以手动
EM_AUTOURLDETECT
向TRichEdit
窗口发送消息,并AURL_ENABLEEMAILADDR
在wParam
参数中指定标志。否则,您必须手动启用超链接。选择所需的文本并
CFE_LINK
对其应用格式样式,您可以通过TRichEdit.SelAttributes.Link
属性(如果您的 Delphi 版本支持该属性)或EM_SETCHARFORMAT
消息来执行此操作。