How to detect URL links after setting EM_AUTOURLDETECT in TRichEdit?
Asked Answered
N

1

6

I'm trying to implement URL detection for TRichEdit component using EM_AUTOURLDETECT message.
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. And that's the problem because my TRichEdit is in ReadOnly mode when applying this feature.

What should I do after performing this code to force the TRichEdit to detect URLs in already written text ?
I was looking at the documentation but there's no mention about something like this.

Thank you

Neogene answered 17/1, 2012 at 16:7 Comment(0)
K
5

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.

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;
Kelleher answered 17/1, 2012 at 16:14 Comment(10)
Thanks TLama but isn't there more clean technique to do it ? Some kind of single notification ? It looks quite "heavy" to clear and set the TRichEdit's text back.Neogene
In documentation there's the following: If automatic URL detection is enabled, the rich edit control scans any modified text to determine whether the text matches the format of a URL. so it seems the text should be modified to get this format, but if there's an easier way (some notification as you say) to force the system to apply CFE_LINK effect for all words already present in rich edit I don't know.Kelleher
no other workaround AFIK. a simplified code would be: SendMessage(RichEdit1.Handle, WM_SETTEXT, 0, Longint(RichEdit1.Text)); after you set EM_AUTOURLDETECTRemoval
@kobik, you're right, thanks. And it will look much more stylish. I'll include it into the answer with excuse ;)Kelleher
I think VCL way to do that is RecreateWnd callCheddite
@user539484, it's impossible because TRichedit has no RecreateWnd published and even if you would like e.g. perform the CM_RECREATEWND message explicitly, nothing happens (what is fair enough because it's only for underlying controls :)Kelleher
@TLama, this method is never supposed to be published. RecreateWnd's purpose is to cope with control styles which cannot be set on live control: Some changes in control properties are not implemented until this re-creation occurs.Cheddite
@user539484, it is published e.g. for form; but anyway RecreateWnd applies changes for some properties though but not for effects which are set explicitly. The TRichedit doesn't store this setting anywhere and if so, you would have to call the EM_AUTOURLDETECT at some creation method or property setter (and if property setter, then you would have to set that property in the creation method) to get it applied.Kelleher
@TLama, it is published e.g. for form – No, it and always been protected. TRichedit doesn't store this setting anywhere – TCustomRichEdit have predefined event mask, if you want to set more flags - send the message in eg: CreateWnd.Cheddite
@user539484, well, RecreateWnd is always protected, but you can call it from unit namespace for TForm so it is published in the meaning of the word (not technically). But still, even RecreateWnd doesn't affect setting of the EM_AUTOURLDETECT without changing text (see my temporary edit).Kelleher

© 2022 - 2024 — McMap. All rights reserved.