Hi Experts, I want to be able to hove the mouse cursor over a specific character in a TRichEdit and have a hint appear displaying simply the index of that particular character within the TRichEdit. Googling around I found some Delphi samples using the EM_CHARFROMPOS call and have implemented something in the "MouseMove" event that *almost* works right, but not quite. The only real problem I'm having with it is if I hover the mouse cursor at empty white space at the far right (unused portion) of the TRichEdit, it still returns the index as the 1st character (ie. index = 0). In this case, I'd like to not display a hint at all, because the cursor is not over a real character. No doubt this is because EM_CHARFROMPOS is zero-based and returns a 0 for the 1st char, 1 for the and char, etc. And that's fine when the mouse is over a legitimate character in the TRichEdit... but when it's over empty space, the EM_CHARFROMPOS still returns a 0 and this is throwing me off. If only the EM_CHARFROMPOS would return a "-1" or something when the mouse is not hovering over a real character, then that would be perfect.
Here is the code of my routine:
procedure TForm1.sRichEdit2MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
iCharIndex, lnth: Integer;
sRichEdit2.hint := ''; // Clear out the old hint
if Length(sRichEdit2.Text) <= 0 then EXIT; // if nothing in TRichEdit, then just exit
with TRichEdit(Sender) do
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;
iCharIndex := iCharIndex + 1;
lnth := Length(sRichEdit2.Text);
if iCharIndex > lnth then EXIT;
if TRichEdit(Sender).Hint <> sHint then
TRichEdit(Sender).Hint := 'Index is '+ sHint;
Any suggestions/thought please...?
P.S: I'm using Delphi 7.