In internet I found how to recognize url in richedit and make it clickable like a link and go to website.
that is the code to detect web links:
var
mask: Word;
begin
mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);
end;
and this is the code to open link:
protected
procedure WndProc(var Message: TMessage); override;
var
p: TENLink;
strURL: string;
begin
if (Message.Msg = WM_NOTIFY) then
begin
if (PNMHDR(Message.lParam).code = EN_LINK) then
begin
p := TENLink(Pointer(TWMNotify(Message).NMHdr)^);
if (p.Msg = WM_LBUTTONDOWN) then
begin
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, Longint(@(p.chrg)));
strURL := RichEdit1.SelText;
ShellExecute(Handle, 'open', PChar(strURL), 0, 0, SW_SHOWNORMAL);
end
end
end;
inherited;
end;
also i found a code to color the text between parenthesis:
procedure HTMLSyntax(Richedit: TRichEdit; TextCol, TagCol, DopCol: TColor);
var
i, iDop: Integer;
s: string;
Col: TColor;
isTag, isDop: Boolean;
begin
iDop := 0;
isDop := False;
isTag := False;
Col := TextCol;
Richedit.SetFocus;
for i := 0 to Length(Richedit.Text) do
begin
Richedit.SelStart := i;
Richedit.SelLength := 1;
s := Richedit.SelText;
if (s = '(') or (s = '{') then
isTag := True;
if isTag then
if (s = '"') then
if not isDop then
begin
iDop := 1;
isDop := True;
end
else
isDop := False;
if isTag then
if isDop then
begin
if iDop = 1 then
Col := DopCol;
end
else
Col := TagCol
else
Col := TextCol;
Richedit.SelAttributes.Color := Col;
iDop := 0;
if (s = ')') or (s = '}') then
isTag := False;
end;
Richedit.SelLength := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RichEdit1.Lines.BeginUpdate;
HTMLSyntax(RichEdit1, clBlue, clRed, clYELLOW);
RichEdit1.Lines.EndUpdate;
end;
the first two codes automatically detect text that starts with 'www.' and treats that as a web link to '.com'. my question is how to use the third code which highlights the text in parenthesis and set it like a web link in first code and shows a message instead of opening browser in second code. to be more clear, suppose there is a scientific text in richedit and you want to give reference at different points like (19). if the user click that text , a message with full reference address apears. thanks.