Главная страница | назад





Article #17602: Caret position in TMemo vs. TRichEdit

 Question and Answer Database

FAQ2602C.txt   Caret position in TMemo vs. TRichEdit
Category   :VCL
Platform    :All
Product    :C++Builder  ALL

Question:
How can I display the position of the caret with a TMemo
component or a TRichEdit component?

Answer:
The implementation is different in each case. In TMemo this code
will work:
  TPoint Point;
  GetCaretPos(&Point);
  DWORD both;

  both = Memo1->Perform(EM_CHARFROMPOS, 0, MAKELPARAM(Point.x, Point.y));
  int indexLine   = HIWORD(both);
  int indexLength = LOWORD(both) - Memo1->Perform(EM_LINEINDEX, -1, 0);

  indexLine++;
  indexLength++;
  char RowCol[10] = " ";
  sprintf(RowCol, "%d:%d", indexLine, indexLength);
  StatusBar1->Panels->Items[4]->Text = RowCol;

 
In TRichEdit, the previous code would produce an access violation
when using EM_CHARFROMPOS.

This code will work for TRichEdit:

  TPoint Point;
  GetCaretPos(&Point);

  DWORD both = 0;
  both = MAKELPARAM(Point.x, Point.y);
  int indexLine = RichEdit1->Perform(EM_LINEFROMCHAR, -1, 0);
  int indexLength = (LOWORD(both)/8) + RichEdit1->SelLength;

  indexLength++;
  indexLine++;
  char RowCol[10] = " ";
  sprintf(RowCol, "%d:%d", indexLine, indexLength);
  StatusBar1->Panels->Items[4]->Text = RowCol;

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99