如何在RichEdit控件里加入链接

6 views
Skip to first unread message

magi...@gmail.com

unread,
Sep 10, 2006, 1:14:30 AM9/10/06
to kicoy开发论坛
其实就是调整一种格式,RichEdit有很多格式,字体啊颜色啊,这种链接格式其实类似下划线再加蓝色,不同的是链接格式是可以触发事件的。


可以设置为url自动检测,这个在选项里可以设置的。

先讲KOL环境下的设置,先选定文字,再设链接:


hintText.SelStart:=idx;

hintText.SelLength:=Length(tagname);

//设置链接

hintText.RE_CharFmtArea:=raSelection;

hintText.RE_FmtLink:=true;

URL响应事件:RE_URLClick

再说VCL下:

设置自动URL

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);

设定选中的文字为链接显示

var
cf2:CHARFORMAT2;
begin
RichEdit1.SelStart:=0;
RichEdit1.SelLength:=4;
ZeroMemory(@cf2,sizeof(CHARFORMAT2));
cf2.cbSize
:= sizeof(CHARFORMAT2);
cf2.dwMask := CFM_LINK;
cf2.dwEffects :=
CFE_LINK;
RichEdit1.Perform(EM_SETCHARFORMAT,
SCF_SELECTION,integer(@cf2));
RichEdit1.SelStart:=length(Richedit1.Text);
end;


设置URL响应事件,截获消息:

if (Msg.Msg = WM_NOTIFY) then
begin
if (PNMHDR(Msg.lParam).code =
EN_LINK) then
begin
p := TENLink(Pointer(TWMNotify(Msg).NMHdr)^);
if (p.Msg =
WM_LBUTTONDOWN) then
begin
try
CE :=
TRichEdit(Form1.ActiveControl);
SendMessage(CE.Handle, EM_EXSETSEL, 0,
Longint(@(p.chrg)));
sURL :=
CE.SelText;
form1.Caption:=sURL;
ShellExecute(Handle, ‘open’, PChar(sURL),
nil, nil, SW_SHOWNORMAL);
except
end;
end;
end;
end;


本文章使用开源内容管理kicoy发布

Reply all
Reply to author
Forward
0 new messages