Solved

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

Posted on 2004-09-30
5
247 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

776 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