0

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;    
RRUZ
  • 134,889
  • 20
  • 356
  • 483
BrownFox97
  • 393
  • 1
  • 3
  • 9

1 Answers1

3

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.

Community
  • 1
  • 1
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • "There is a rather subtle bug" - do you remember what this bug is? Subtle bugs can be hard to find sometimes, especially if you're not used to the code area, and since he's asking I'm guessing BrownFox isn't :) – David Dec 20 '10 at 05:19
  • @David M: Yes, with the aid of Google I found a text (written by myself and Sertac Akyuz) that pinpoints the bug(s). – Andreas Rejbrand Dec 20 '10 at 11:56