TMemo - (RichEdit) How to get the charater position of the mouse. lazarus / 31-LCL ---------------------------------------------------------------------- arneolav · 2013-12-19 21:42 win XP, Lazarus 1.2.RC1, FPC2.6.2 Hi I need to get the character position of the mouse in TMemo/RichEdit. The idea is, in a rich edit to get the 'word under the mouse' , and show alternate tranlations of the word This code from Delphi: Code: [Select]procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;   Shift: TShiftState; X, Y: Integer); var  Pt: TPoint; CharIndex, Col, Row: Integer; begin   Pt:= Point(X, Y);   With Memo1 do begin       CharIndex := Perform(EM_CHARFROMPOS, 0, Integer(@Pt));       Row  := Perform(EM_EXLINEFROMCHAR, 0, CharIndex);       Col  := CharIndex - Perform(EM_LINEINDEX, Row, 0);   end;       StatusBar1.Panels.Items[0].Text:= ' X: ' + inttostr(X) + ' Y: ' + Inttostr(Y)                                           + ' CharIndex: ' + inttostr(CharIndex)                                           + ' Col: ' + inttostr(Col)                                           + ' Row: ' + inttostr(Row);   end;  Mouse X and Y pos are correct. CharaterPosition = 0, and then Row and Col are 0.... Delphi mode gives same result Any idea? ---------------------------------------------------------------------- engkin · 2013-12-19 23:07 EM_CHARFROMPOS does not take a pointer to POINT. It takes a Long (4 bytes) where X and Y are combined. Its lower Word (2 bytes) has the X value and its higher Word has the Y value. The result is also two combined values: Code: [Select]var   Pt: Integer;   CharLineIndex: DWord;   CharIndex, LineIndex: Word; ...   Pt:=(X and $FFFF) or ((Y and $FFFF) shl 16);// Combine X and Y values   CharLineIndex := SendMessage(Memo1.Handle,EM_CHARFROMPOS, 0, Pt);   CharIndex := CharLineIndex and $FFFF;   LineIndex := (CharLineIndex shr 16) and $FFFF; ... You used Perform instead of Memo1.Perform in the other two calls. Edit: You don't need EM_EXLINEFROMCHAR because EM_CHARFROMPOS has given you the line index already. Code: [Select]procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,   Y: Integer); var   Pt: Integer;   CharLineIndex: DWord;   CharIndex, LineIndex: Word;   Col: Integer; begin   Pt:=(X and $FFFF) or ((Y and $FFFF)shl 16);   CharLineIndex := SendMessage(Memo1.Handle,EM_CHARFROMPOS, 0, Pt);   CharIndex := CharLineIndex and $FFFF;   LineIndex := (CharLineIndex shr 16) and $FFFF;   Col  := CharIndex - SendMessage(Memo1.Handle, EM_LINEINDEX, LineIndex, 0);   StatusBar1.Panels.Items[0].Text:= ' X: ' + inttostr(X) + ' Y: ' + Inttostr(Y)                                       + ' CharIndex: ' + inttostr(CharIndex)                                       + ' LineIndex: ' + inttostr(LineIndex)                                       + ' Col: ' + inttostr(Col); end; ---------------------------------------------------------------------- arneolav · 2013-12-20 09:57 Thanks' again engkin This is perfect in TMemo. .... But when I move it to RichMemo or LzRichEdit ,... CharIndex allways = CharLineIndex and LineIndex=0, (col=0 or col=CharIndex) And ..when mouse moves into "whitespace" I get a dump: 4B41821A 8b10                     mov    (%eax),%edx ..... By an "accident" in my testbed; I foregot to change the names from Memo1 to RichMemo/LzRichEdit in the SendMessage's. With a Memo1 defined in the same unit as the RichMemo or LzRichEdit, then the col/row are perfect in  RichMemo and RichEdit. (The Memo1 has to be of same size or greater than the RichMemos). I could use this feature as a workaround by just defina a fake/invisible TMemo, but if possible, a correct solution is preferable. ---------------------------------------------------------------------- engkin · 2013-12-20 14:22 You are right, your original code should work with RichEdit.  The changes I mentioned are for TMemo. You just need to correct minor issues. Try this code on a RichEdit: Code: [Select]var   Pt: TPoint; CharIndex, Col, Row: Integer; begin   Pt:= Point(X, Y);   CharIndex := RichEdit1.Perform(EM_CHARFROMPOS, 0, Integer(@Pt));   Row  := RichEdit1.Perform(EM_EXLINEFROMCHAR, 0, CharIndex);   Col  := CharIndex - RichEdit1.Perform(EM_LINEINDEX, Row, 0);   StatusBar1.Panels.Items[0].Text:= ' X: ' + inttostr(X) + ' Y: ' + Inttostr(Y)                                       + ' CharIndex: ' + inttostr(CharIndex)                                       + ' Col: ' + inttostr(Col)                                       + ' Row: ' + inttostr(Row); end; This is your code with the fixes. Sorry for the confusion. ---------------------------------------------------------------------- arneolav · 2013-12-20 15:36 No  confusion, i asked for TMemo  and RichEdit .  In my head the Rich Edit is build on TMemo, but it seems I'm wrong... And my Delphi-code was not all perfect. It's corrected. Never mind;  Hmmm CharIndex := RichEdit1.Perform(EM_CHARFROMPOS, 0, Integer(@Pt)); don't work, Result; CharIndex = 0... Tested in TRichMemo and TLzRichEdit..  result allways zero ---------------------------------------------------------------------- arneolav · 2013-12-22 20:44 This seems to be correct to find the character position  (Col, Row)  of mouse in a text  in RichMemo and LzRichEdit: Code: [Select]procedure TForm1.LzRichEditor1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);  var  Pt: TPoint;         CharIndex, Col, Row : Integer; begin   Pt:= Point(X, Y);   CharIndex := SendMessage(LzRichEditor1.Handle, EM_CHARFROMPOS,  0, Integer(@Pt));   Row       := SendMessage(LzRichEditor1.Handle, EM_EXLINEFROMCHAR, 0, CharIndex);   Col       := CharIndex - SendMessage(LzRichEditor1, Handle, EM_LINEINDEX, Row, 0);       StatusBar1.Panels.Items[1].Text:= ' X: ' + inttostr(X)                                       + ' Y: ' + Inttostr(Y)                                 +' CharIndex: ' + inttostr(CharIndex)                                 + ' Col: ' + inttostr(Col)      // GetOffSetIndent=0                                 +' Row: ' + inttostr(Row);  end; ---------------------------------------------------------------------- tranminhchien · 2013-12-29 13:08 You should use a TSynEdit, because it contain more useful function than a memo With TSynEdit, use can get the position of the mouse by : Code: [Select]x := SynEdit1.CaretXPix; y := SynEdit1.CaretYPix; ---------------------------------------------------------------------- arneolav · 2013-12-29 15:32 Well, I tried SynEdit, but it seems not be a good texteditor. At least two problems: Do not read .rtf files correct. Don't have linebreak. If this is possible, i'll try it again.