Solved

Visible lines in a RichEditCtrl

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now