Solved

Visible lines in a RichEditCtrl

Posted on 1998-10-28
3
382 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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