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

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.
PETERJORDANAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eddie ShipmanAll-around developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PETERJORDANAuthor Commented:
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
DragonSlayerCommented:
EM_CHARFROMPOS is defined in the winuser.h file.
0
Eddie ShipmanAll-around developerCommented:
[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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.