c++buildervcltricheditc++builder-xe5

RichEdit get line from pixel position


If I click inside a TRichEdit control, how can I get the Line index (I clicked on) from the mouse client coordinates?

In other words, how do I convert a client coordinate to a Line Index?

***EDIT I'm actually not clicking inside the TRichEdit control, I'm clicking in another control and need to synch with the TRichEdit control's line number.


Solution

  • Send EM_CHARFROMPOS and EM_LINEFROMCHAR messages to the RichEdit, eg:

    POINTL pt = ...; // client coordinates
    int pos = RichEdit1->Perform(EM_CHARFROMPOS, 0, (LPARAM)&pt);
    int idx = RichEdit1->Perform(EM_LINEFROMCHAR, pos, 0);