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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Eddie ShipmanConnect With a Mentor All-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
 
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
All Courses

From novice to tech pro — start learning today.