Solved

How to find the font descritpion of a word in a richedit?

Posted on 2004-09-30
5
241 Views
Last Modified: 2013-12-03
Hi team,
I have a richedit box and I have programmed into it the ability to change the font size, style, name etc.
When I click my the mouse onto a word in this richedit box I would like to be able to determine all the details of the selected word. I found some code on the net which I have adapted to suit my purposes. see below:

uses richedit;

procedure TfrmWriter.richEditorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  WhatStyle(X, Y);
end;

procedure TfrmWriter.WhatStyle(X, Y: Integer);
//Purpose : To make show what style of text is currently where the mouse pointer is
var
  iCharIndex, iLineIndex, iCharOffset, i, j: Integer;
  Pt: TPoint;
  s: string;
begin
with richeditor do
  begin
    Pt := Point(X, Y);
    // Get Character Index from word under the cursor
    iCharIndex := Perform(Messages.EM_CHARFROMPOS, 0, Integer(@Pt));
    if iCharIndex < 0 then Exit;
    // Get line Index
    iLineIndex  := Perform(EM_EXLINEFROMCHAR, 0, iCharIndex);
    iCharOffset := iCharIndex - Perform(EM_LINEINDEX, iLineIndex, 0);
    if Lines.Count - 1 < iLineIndex then Exit;
    // store the current line in a variable
    s := Lines[iLineIndex];
    // get the first letter of the selected word
    i := iCharOffset + 1;
    SelStart := i;
    label1.Caption := SelAttributes.Name;
    label2.Caption := inttostr(SelAttributes.size);
    label3.Caption := inttostr(SelAttributes.Color);

    if SelAttributes.Style = [fsbold] then
    begin
      label4.Caption := 'Bold'
    end
    else
    begin
      label4.Caption := 'Not Bold'
    end;

    if SelAttributes.Style = [fsItalic] then
    begin
      label5.Caption := 'Italic'
    end
    else
    begin
      label5.Caption := 'Not fsItalic'
    end;

    if SelAttributes.Style = [fsUnderline] then
    begin
      label6.Caption := 'fsUnderline'
    end
    else
    begin
      label6.Caption := 'Not fsUnderline'
    end;
  end;
end;

If I comment out the functioncall WhatStyles, then my rich edit control works fine and I can move the insertion point all around the control. But for some strange reason, when I include the function then it seems to limit where I can move the insertion point to only the first couple of lines ???

I also think I am not using the selattributes.styles property properly as the output in the captions for bold, italic and underline is not consistent.

Does anyone have any ideas?

thanks

Myles :)
0
Comment
Question by:myleseven
  • 4
5 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12204511
You can fix the font styles like so:

if fsUnderline in SelAttributes.Style then
  label6.caption:='fsUnderline'
else
  ....

The Style property is a "set" of attributes, so:

if fsBold in SelAttributes then ..

is true if bold is in the set...
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12204525
The WhatStyles procedure is moving the cursor with the

SelStart:=

call, which is why you're having trouble.

If you move the whole routine to the OnSelectionChanged() event of the rich edit, I believe you'll have the correct results.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12204536
Oh, in OnSelectionChange() event, you won't need anything from the SelStart line up in your WhatStyles routine. SelStart already has the cursor position if nothing is selected.
0
 
LVL 7

Accepted Solution

by:
LRHGuy earned 100 total points
ID: 12204574
procedure TfrmHelpEditor.eFile1SelectionChange(Sender: TObject);
begin
with RichEditor  do
  begin
    label1.Caption := SelAttributes.Name;
    label2.Caption := inttostr(SelAttributes.size);
    label3.Caption := inttostr(SelAttributes.Color);

    if fsBold in SelAttributes.Style then
    begin
      label4.Caption := 'Bold'
    end
    else
    begin
      label4.Caption := 'Not Bold'
    end;

    if fsItalic in SelAttributes.Style then
    begin
      label5.Caption := 'Italic'
    end
    else
    begin
      label5.Caption := 'Not fsItalic'
    end;

    if fsUnderline in SelAttributes.Style then
    begin
      label6.Caption := 'fsUnderline'
    end
    else
    begin
      label6.Caption := 'Not fsUnderline'
    end;
  end;
end;
0
 

Author Comment

by:myleseven
ID: 12213167
That was awesom thank LRHGuy, I leaned something today :)

Myles
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now