抱歉,如果下面的格式有点不对劲。
尝试从 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;