whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

12 Visitors Online


 
...get the word under the mouse cursor in a TRichEdit?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (9):  
     


uses
 
RichEdit;
 
procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  
iCharIndex, iLineIndex, iCharOffset, i, j: Integer;
  Pt: TPoint;
  s: string;
begin
  with 
TRichEdit(Sender) 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];
    // Search the beginning of the word
    
i := iCharOffset + 1;
    while (i > 0) and (s[i] <> ' ') do Dec(i);
    // Search the end of the word
    
j := iCharOffset + 1;
    while (j <= Length(s)) and (s[j] <> ' ') do Inc(j);
    // Display Text under Cursor
    
Caption := Copy(s, i, j - i);
  end;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners