Solved

Visible lines in a RichEditCtrl

Posted on 1998-10-28
3
385 Views
Last Modified: 2013-11-20
How do you find out which lines are visible in a richedit contol.
I've seen CRichEditCtrl::GetFirstVisibleLine() but where can I get the rest of the information ?
0
Comment
Question by:Bridge
  • 2
3 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 1323941
void COpenerDlg::OnButton1()
{
   // obviously, need a pointer to the control
   CRichEditCtrl* pCtrl = (CRichEditCtrl*) GetDlgItem(IDC_RICHEDIT1);
   ASSERT(pCtrl != NULL);

   // get the dimensions of the control
   CRect rect;
   pCtrl->GetClientRect(rect);

   // find the position of the character closest to the bottom right of the control
   POINTL pt;
   pt.x = rect.right;
   pt.y = rect.bottom - 10;   //FUDGE FACTOR!!
   LRESULT l = pCtrl->SendMessage(EM_CHARFROMPOS, 0, (LPARAM) &pt);

   // decompress the result
   int nLastChar = LOWORD(l);

   // get the top visible line
   int nTopChar = pCtrl->LineIndex(pCtrl->GetFirstVisibleLine());
   ASSERT(nTopChar <= nLastChar);

   // get the text from nTopChar to nLastChar
   CString str;

   TEXTRANGE textRange;
   textRange.chrg.cpMin = nTopChar;
   textRange.chrg.cpMax = nLastChar;
   textRange.lpstrText = str.GetBuffer(nLastChar - nTopChar + 2);
   pCtrl->SendMessage(EM_GETTEXTRANGE, 0, (LPARAM) &textRange);
   str.ReleaseBuffer();

   MessageBox(str);

   return;
}


Note the fudge factor in converting the rectangle. The EM_CHARFROMPOS code gets the "character nearest the point", so you might get an extra line at the end depending on the size of your window, the length of the last visible line, and the height of the font.

The EM_CHARFROMPOS message is your only hope, so you have to make it work...  If you need the function to be super-reliable, you can call EM_POSFROMCHAR on the best-guess index and work backward until you're completely sure the character you're considering to be the last really is visible.

B ekiM

0
 
LVL 1

Author Comment

by:Bridge
ID: 1323942
I am using a CRichEditView. Whenever I try to do a SendMessage( EM_CHARFROMPOS, ...) I get and access violation in the RTF DLL. It makes no difference if I send the message to the view or the underlying control.
Any ideas ??
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 1323943
That's caused by a bug in the system headers.  See Knowledge Base articles Q137805, Q137249, and Q68572 for the official workarounds.

Also, try using a POINT structure instead of a POINTL structure, as I did above.

B ekiM


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question