Board index » delphi » TRichEdit detecting font attributes under cursor

TRichEdit detecting font attributes under cursor

I am trying to detect not the text, but the text attributes under the mouse
cursor in a TRichEdit.

I assume that this is possible, but I cannot find any method in Delphi or in
the API that seems to do this.

At the moment I am using a cheat by having a second and hidden TRichEdit.
First I detect the position of the text under the cursor then I copy the
whole of the text to the hidden richedit and then I move the cursor in the
hidden richedit accordingly.  In this way, I can detect the text attributes
of the second richedit.  This seems to work well enough and is very fast
even in large text documents.  But it seems to me to be a lot of work for
something that should really be quite simple.

I am trying to achieve the same effect as the cursor passing over a
hypertext link as you see in various wordprocessing packages.

Can anyone help please.

Mark Williams

 

Re:TRichEdit detecting font attributes under cursor


This code, which I don't have the time to comment, contains your answer.

- John Bleau

procedure TfRE.richedit1SelectionChange(Sender: TObject);
var zI: integer;
begin
  with richedit1 do
  begin
    if cbFont.text<>SelAttributes.Name then
      cbFont.text:=SelAttributes.Name;
    if cbSize.text<>inttostr(SelAttributes.Size) then
      cbSize.text:=inttostr(SelAttributes.Size);
    if SelAttributes.Color<>Tag then
    begin
      Tag:=SelAttributes.Color;
      if gC[lColor.tag].color<>SelAttributes.Color then
      begin
        lColor.caption:='';
        lColor.tag:=0;
        for zI:=1 to high(gC) do
          if gC[zI].Color=SelAttributes.Color then
          begin
            lColor.caption:=gC[zI].Name;
            lColor.tag:=zI;
            break;
          end;
        if lColor.tag=0 then
        if SelAttributes.Color=-2147483640 then //black
        begin
          lColor.tag:=1;
          lColor.caption:=gC[lColor.tag].name;
        end;
      end;
    end;
  end;
end;

Re:TRichEdit detecting font attributes under cursor


John,

Thanks for your help, although I don't think I made myself clear.  What I
need to be able to do is detect the text under the cursor on a MouseMove
event.  I do not want to have to select or click on the text.

Thanks

Mark Williams

Quote
John Bleau <jrbleauNOS...@sympatico.ca> wrote in message

news:3ccc66d4$1_2@dnews...
Quote
> This code, which I don't have the time to comment, contains your answer.

> - John Bleau

> procedure TfRE.richedit1SelectionChange(Sender: TObject);
> var zI: integer;
> begin
>   with richedit1 do
>   begin
>     if cbFont.text<>SelAttributes.Name then
>       cbFont.text:=SelAttributes.Name;
>     if cbSize.text<>inttostr(SelAttributes.Size) then
>       cbSize.text:=inttostr(SelAttributes.Size);
>     if SelAttributes.Color<>Tag then
>     begin
>       Tag:=SelAttributes.Color;
>       if gC[lColor.tag].color<>SelAttributes.Color then
>       begin
>         lColor.caption:='';
>         lColor.tag:=0;
>         for zI:=1 to high(gC) do
>           if gC[zI].Color=SelAttributes.Color then
>           begin
>             lColor.caption:=gC[zI].Name;
>             lColor.tag:=zI;
>             break;
>           end;
>         if lColor.tag=0 then
>         if SelAttributes.Color=-2147483640 then file://black
>         begin
>           lColor.tag:=1;
>           lColor.caption:=gC[lColor.tag].name;
>         end;
>       end;
>     end;
>   end;
> end;

Re:TRichEdit detecting font attributes under cursor


In <3ccc5b7d_2@dnews>, Mark Williams wrote:

Quote
> I am trying to detect not the text, but the text attributes under the
> mouse
> cursor in a TRichEdit.

> I assume that this is possible, but I cannot find any method in
> Delphi or in
> the API that seems to do this.

> I am trying to achieve the same effect as the cursor passing over a
> hypertext link as you see in various wordprocessing packages.

Mark

If it's just hypertext links that you're trying to detect, you can do
that with third-party implementations of richedit - RichEdit98, for
example - which handles the EN_Link message in version 2 of the MS
control.

If you're looking for arbitrary text attributes, then the best bet
would be to use the text object model (tom) interface, which gives you
access to attributes of character ranges other than just the current
selection.  But again, you'll need to have access to something that
implements at least version 2.

Roger Morton
ro...@chez-morton.com

Re:TRichEdit detecting font attributes under cursor


Roger,

Thanks for your help.

Any idea where I can get infor on TOM?

Mark

Quote
Roger Morton <ro...@xnospamxchez-morton.com> wrote in message

news:dYDR4VtWEdaWmwDA8DgiHw@LOFT...
Quote
> In <3ccc5b7d_2@dnews>, Mark Williams wrote:

> > I am trying to detect not the text, but the text attributes under the
> > mouse
> > cursor in a TRichEdit.

> > I assume that this is possible, but I cannot find any method in
> > Delphi or in
> > the API that seems to do this.

> > I am trying to achieve the same effect as the cursor passing over a
> > hypertext link as you see in various wordprocessing packages.

> Mark

> If it's just hypertext links that you're trying to detect, you can do
> that with third-party implementations of richedit - RichEdit98, for
> example - which handles the EN_Link message in version 2 of the MS
> control.

> If you're looking for arbitrary text attributes, then the best bet
> would be to use the text object model (tom) interface, which gives you
> access to attributes of character ranges other than just the current
> selection.  But again, you'll need to have access to something that
> implements at least version 2.

> Roger Morton
> ro...@chez-morton.com

Re:TRichEdit detecting font attributes under cursor


In <3cce5810_1@dnews>, Mark Williams wrote:

Quote

> Any idea where I can get infor on TOM?

It's fully documented in the MS Platform SDK Help files - look for the
ITextDocument and ITextRange interfaces.

As long as you've got riched20.dll (which implements v2 or v3 of the MS
control) on the relevant machines, you'll have access to TOM.  Delphi's
TRichEdit wraps riched32.dll, which is v1.

You could also look for news:3cc57233$1_1@dnews in b.p.d.winapi -
'Unicode selection from RichEdit' from Steve Moss last week; in the
responses in that thread I explained to him how to get into TOM for
other purposes.

Roger Morton
ro...@chez-morton.com

Other Threads