Domanda

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

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top