Solved

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

Posted on 2004-09-30
5
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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