如何在丰富的编辑中查找带下划线的文本?
如果下面的格式有点不对,敬请谅解。
尝试从 Richedit 控件获取带下划线的文本,以确定单击时它是否是超链接。
该代码适用于 Delphi 2007 及以下版本。我知道有一个 TCharFormat2 结构,并且字符编码可能已更改。
但还没有改变这些的运气。
非常感谢任何帮助。谢谢。
----------------------------------------
function GetUnderlinedText( ARichEdit: TRichEdit; CharIdx: Integer ): String;
var
i: Integer;
CharFormat: TCharFormat;
SelStart: Integer;
begin
CharFormat.cbSize := SizeOf( TCharFormat );
CharFormat.dwMask := CFM_UNDERLINE;
ARichEdit.SelStart := CharIdx;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//------- If not underlined return empty str. ------------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Result := '';
Exit;
end;
//--------- Find Beginning of Underlined Text ------------
i := CharIdx;
while (i>0) do
begin
ARichEdit.SelStart := i;
//------------ Check for New Line Char -----------------
if( ARichEdit.Text[i]=#10 ) then
Break;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//----------- Test if Character was Underlined ---------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Break;
end;
Dec( i );
end;
//------------ Find Length of Underlined Text ------------
SelStart := i;
i:=1;
while (SelStart+i &< Length( ARichEdit.Text ) ) do //subtract the & from line
begin
ARichEdit.SelStart := SelStart + i;
//------------ Check for New Line Char -----------------
if( ARichEdit.Text[SelStart+i]=#10 ) then
Break;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//----------- Test if Character was Underlined ---------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Break;
end;
Inc( i );
end;
ARichEdit.SelStart := SelStart;
ARichEdit.SelLength := i;
Result := Trim(ARichEdit.SelText);
ShowMessage( Result ); //Seems to be showing only part of the underlined text
end;
Apologies if the formatting below is a bit off.
Trying to get the underlined text from a Richedit control to determine if it's a hyperlink when clicked.
This code worked in Delphi 2007 and below. I know there's a TCharFormat2 structure and the character encoding may have changed.
Haven't had any luck changing these though.
Any help greatly appreciated. Thanks.
----------------------------------------
function GetUnderlinedText( ARichEdit: TRichEdit; CharIdx: Integer ): String;
var
i: Integer;
CharFormat: TCharFormat;
SelStart: Integer;
begin
CharFormat.cbSize := SizeOf( TCharFormat );
CharFormat.dwMask := CFM_UNDERLINE;
ARichEdit.SelStart := CharIdx;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//------- If not underlined return empty str. ------------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Result := '';
Exit;
end;
//--------- Find Beginning of Underlined Text ------------
i := CharIdx;
while (i>0) do
begin
ARichEdit.SelStart := i;
//------------ Check for New Line Char -----------------
if( ARichEdit.Text[i]=#10 ) then
Break;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//----------- Test if Character was Underlined ---------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Break;
end;
Dec( i );
end;
//------------ Find Length of Underlined Text ------------
SelStart := i;
i:=1;
while (SelStart+i &< Length( ARichEdit.Text ) ) do //subtract the & from line
begin
ARichEdit.SelStart := SelStart + i;
//------------ Check for New Line Char -----------------
if( ARichEdit.Text[SelStart+i]=#10 ) then
Break;
SendMessage( ARichEdit.Handle, EM_GETCHARFORMAT, 1, Integer( @CharFormat ) );
//----------- Test if Character was Underlined ---------
if (CharFormat.dwEffects and CFE_UNDERLINE)=0 then
begin
Break;
end;
Inc( i );
end;
ARichEdit.SelStart := SelStart;
ARichEdit.SelLength := i;
Result := Trim(ARichEdit.SelText);
ShowMessage( Result ); //Seems to be showing only part of the underlined text
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实知道可以让 Rich Edit 控件自动检测 URL,对吗?该控件将自动突出显示超链接,并在单击此类超链接时向您发送一条消息。此功能不是由 VCL 包装器提供的,但可以通过访问底层 Windows API 轻松启用。例如,可以在此处找到详细信息:
如果我没记错的话,上面的 Scalabium 代码片段中有一个相当微妙的错误,但是借助优秀的 MSDN 文档,我相信您会找到它。
更新
是的,我没记错。 此处讨论了 Scalabium 代码中的错误。
更新 2
幸运的是,Scalabium 上的错误似乎已得到纠正。
You do know that you can make the rich edit control automatically detect URLs, right? The control will automatically highlight hyperlinks, and will send you a message when such a hyperlink is clicked. This functionality is not provided by the VCL wrapper, but is easily enabled by reaching out to the underlying Windows API. Details are found, for instance, here:
If I recall correctly, there is a rather subtle bug in the Scalabium code snippet above, but with the aid of the excellent MSDN documentation, I am sure you will find it.
Update
Yes, I did recall correctly. The bugs in the Scalabium code are discussed here.
Update 2
Fortunately, it appears as if the bugs on Scalabium have been corrected.