简体   繁体   English

如何在TRichEdit中设置EM_AUTOURLDETECT后检测URL链接?

[英]How to detect URL links after setting EM_AUTOURLDETECT in TRichEdit?

I'm trying to implement URL detection for TRichEdit component using EM_AUTOURLDETECT message. 我正在尝试使用EM_AUTOURLDETECT消息为TRichEdit组件实现URL检测。
I have a problem with the following code 我有以下代码的问题

procedure TForm1.Button1Click(Sender: TObject);
var Mask: Word;
begin
  Mask := SendMessage(Handle, EM_GETEVENTMASK, 0, 0);
  SendMessage(Handle, EM_SETEVENTMASK, 0, Mask or ENM_LINK);
  SendMessage(Handle, EM_AUTOURLDETECT, Integer(True), 0);
end;

It works though but I have to change the TRichEdit's text after these settings to get the it detect the URLs in already written text. 它虽然有效,但我必须在这些设置之后更改TRichEdit的文本,以使其检测已写入文本中的URL。 And that's the problem because my TRichEdit is in ReadOnly mode when applying this feature. 这就是问题,因为我的TRichEdit在应用此功能时处于ReadOnly模式。

What should I do after performing this code to force the TRichEdit to detect URLs in already written text ? 执行此代码后,如何强制TRichEdit检测已写入文本中的URL,我该怎么办?
I was looking at the documentation but there's no mention about something like this. 我正在查看文档,但没有提到这样的事情。

Thank you 谢谢

I've had the same problem some time ago and used (quite) a dirty workaround for it. 我前段时间遇到了同样的问题并使用了(相当)一个肮脏的解决方法。 After sending of the EM_AUTOURLDETECT message I get and store the current selection, then (re)set the rich edit's text and set back the selection stored before. 发送EM_AUTOURLDETECT消息后,我获取并存储当前选择,然后(重新)设置富编辑的文本并设置之前存储的选择。

procedure TForm1.Button1Click(Sender: TObject);
var
  EventMask: Word;
  CharRange: TCharRange;
begin
  EventMask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
  SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, EventMask or ENM_LINK);
  SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, WPARAM(True), 0);
  SendMessage(RichEdit1.Handle, EM_EXGETSEL, 0, LPARAM(@CharRange));
  SendMessage(RichEdit1.Handle, WM_SETTEXT, 0, LPARAM(RichEdit1.Text));
  SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LPARAM(@CharRange));
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM