[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-25
8
Medium Priority
?
317 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
Comment
Question by:PETERJORDAN
[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
8 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 1000 total points
ID: 12405067
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
ID: 12406544
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
ID: 12409075
EM_CHARFROMPOS is defined in the winuser.h file.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12410443
[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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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