Solved

How can I get the word over the mouse position in the RichEdit component ? (250pts)

Posted on 2004-10-25
306 Views
Last Modified: 2013-11-22
Hello,

I'd like to know if is there possible to make a function that return a single word in the RichEdit component just passing X and Y mouse coordinates.

Regards,

Peter Jordan.
0
Question by:PETERJORDAN
    5 Comments
     
    LVL 12

    Expert Comment

    by:esoftbg
    0
     
    LVL 26

    Accepted Solution

    by:
    thisone will also show you the attributes of the text:
    function GetFontStyle(Format: TCharFormat2): String;
    var
        LBold:       Boolean; {CFE_BOLD}
        LItalic:     Boolean; {CFE_ITALIC}
        LUnderline:  Boolean; {CFE_UNDERLINE}
        LStrikeOut:  Boolean; {CFE_STRIKEOUT}
    begin
        LBold      := False;
        LItalic    := False;
        LUnderline := False;
        LStrikeOut := False;
        Result := '';
        if ( Format.dwMask and CFM_BOLD ) > 0 then
            LBold := (Format.dwEffects and CFE_BOLD) > 0;
        if ( Format.dwMask and CFM_ITALIC ) > 0 then
            LItalic := (Format.dwEffects and CFE_ITALIC) > 0;
        if ( Format.dwMask and CFM_UNDERLINE ) > 0 then
            LUnderline := (Format.dwEffects and CFE_UNDERLINE) > 0;
        if ( Format.dwMask and CFM_STRIKEOUT ) > 0 then
            LStrikeOut := (Format.dwEffects and CFE_STRIKEOUT) > 0;
        if LBold then
            Result := Result + ' Bold,';
        if LItalic then
            Result := Result + ' Italic,';
        if LUnderline then
            Result := Result + ' Underline,';
        if LStrikeOut then
            Result := Result + ' StrikeOut';
        Result := Trim(Result);
    end;

    function TForm1.GetTheWordUnderTheMouse(RE: TRichEdit; pt: TPoint): String;
      function GetWordFromCharIndex(Line: String; Index: Integer): String;
      var
        i, iBeg, iEnd: Integer;
        temp: String;
      begin
        iBeg := 0;
        iEnd := Length(Line);
        // Find space at end of word
        for i := index to Length(Line) do
        begin
          if Trim(Line[i]) = '' then
          begin
            iEnd := i;
            Break;
          end;
        end;
        // find space at beg of word
        for i := index downto 0 do
        begin
          if Trim(line[i]) = '' then
          begin
            iBeg := i+1;
            Break;
          end;
        end;
        Result := Copy(Line, iBeg, iEnd-iBeg);
      end;

    var
      i, charindex, lineindex, charoffset, iWordBeg, iWordEnd: Integer;
      sWord, sWordbeg: String;
      XFormat: TCharFormat2;
      clr1, clr2: String;
      charIndex2:  Integer;
    begin
      charindex := RE.Perform(Messages.EM_CHARFROMPOS, 0,
                              integer(@pt));
      if charindex >= 0 then
      begin
        lineindex := RE.Perform(EM_EXLINEFROMCHAR, 0, charindex);
        charoffset := charindex - RE.Perform( EM_LINEINDEX, lineindex, 0 );
        sWord := RE.Lines[lineindex];
        if Length(Trim(sWord)) > 0 then
        begin
          sWord := GetWordFromCharIndex(sWord, charoffset);
        end;
      end;
      charIndex2 := RE.SelStart;
      RE.SelStart := charindex;
      FillChar(XFormat, SizeOf(XFormat), 0);
      XFormat.cbSize:=sizeOf(TCharFormat2);
      SendMessage(RE.Handle, EM_GETCHARFORMAT,
                  SCF_SELECTION, LPARAM(@XFormat));
      RE.SelStart := charindex2;
      ColorToIdent(XFormat.crTextColor, clr1);
      ColorToIdent(XFormat.crBackColor, clr2);
      Label1.Caption := 'Text:' + clr1 + #13 + 'Back:'+ clr2 + #13 + 'Style:' + GetFontStyle(XFormat);
      Result := sWord;
    end;

    procedure TForm1.REMouseMove(Sender: TObject; Shift: TShiftState;
                                        X, Y: Integer);
    var
      pt: TPoint;
    begin
      pt:= Point(X, Y);
      if not (ssCtrl in Shift) then
        Label2.Caption := GetTheWordUnderTheMouse(Sender as TRichEdit, pt);
    end;
    0
     

    Author Comment

    by:PETERJORDAN
    Eddie,

    that's exacly what I wanted!!! Actually I am working with C++ Builder because I don't know pascal and I am trying to translate your code to C++. I just can't translate "Messages.EM_CHARFROMPOS" to C++ because I have no idea what is that.
    And by the way, is there a way to get the color of the word as well ?

    Thanks!

    Peter.
    0
     
    LVL 14

    Expert Comment

    by:DragonSlayer
    EM_CHARFROMPOS is defined in the winuser.h file.
    0
     
    LVL 26

    Expert Comment

    by:EddieShipman
    [quote]is there a way to get the color of the word as well [/quote]
    That is what this code does: ColorToIdent(XFormat.crTextColor, clr1);

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    856 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

    18 Experts available now in Live!

    Get 1:1 Help Now