以下のフォーマットが少しずれている場合はお詫び申し上げます。
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;