3

I have a CMyRichEditCtrl class inheriting from CRichEditCtrl. When I call SetSel, it automatically scrolls the contents of the CRichEditCtrl so that the caret is visible. I would like to avoid this behavior.

What is bugging me it that this behavior seems to have changed between 6.0 and other versions.

Visual Studio 2010 : http://msdn.microsoft.com/en-us/library/4zek9k1f(v=vs.100).aspx

The caret is placed at the end of the selection indicated by the greater of the start (cpMin or nStartChar) and end (cpMax or nEndChar) indices. This function scrolls the contents of the CRichEditCtrl so that the caret is visible.

Visual Studio 6.0: http://msdn.microsoft.com/en-us/library/aa313352(v=vs.60).aspx

The caret is placed at the end of the selection indicated by the greater of the start (cpMin or nStartChar) and end (cpMax or nEndChar) indices. This function does not scroll the contents of the CRichEditCtrl so that the caret is visible.

Is there a way to prevent the auto-scroll of the control when calling SetSel ?

MasterMind
  • 379
  • 2
  • 17

3 Answers3

3

This was not an easy one, but I finally found a workaround.

void CMyRichEditCtrl::doStuff()
{
    SetRedraw( FALSE );

    int nOldFirstVisibleLine = GetFirstVisibleLine();

    // Save current selection
    long lMinSel, lMaxSel;
    GetSel( lMinSel, lMaxSel );

    // Do something here
    doSomething();

    // Restore selection
    SetSel( lMinSel, lMaxSel );

    // Prevent the auto-scroll of the control when calling SetSel()
    int nNewFirstVisibleLine = GetFirstVisibleLine();

    if( nOldFirstVisibleLine != nNewFirstVisibleLine )
        LineScroll( nOldFirstVisibleLine - nNewFirstVisibleLine );

    SetRedraw( TRUE );

    RedrawWindow();
 }
MasterMind
  • 379
  • 2
  • 17
  • 2
    I would like to add here that hiding and showing selection using HideSelection() in the above block will drastically improve the performance. I just discovered it. – Gautam Jain Oct 22 '20 at 10:06
0

Use CRichEditCtrl::SetOptions method or the below code to disable and enable auto-scroll. hwnd is the handled to the rich edit control.

You can use the following code to disable auto-scroll:

LRESULT prevOptions = SendMessage(hwnd, EM_GETOPTIONS, 0, 0);
SendMessage(hwnd, EM_SETOPTIONS, ECOOP_SET, prevOptions & ~ECO_AUTOVSCROLL);

And enable it back using:

SendMessage(hwnd, EM_SETOPTIONS, ECOOP_SET, prevOptions);
Gautam Jain
  • 6,789
  • 10
  • 48
  • 67
-2

Change to

RedrawWindow(0,0,RDW_NOERASE);

It's better.

  • 1
    You should add this as a comment to the previos answer from @MasterMind, not as a new answer as this doesn't answer the question of the OP. – Roland Bär Dec 06 '13 at 08:38
  • @user3073563: When I replace RedrawWindow() by RedrawWindow(0,0,RDW_NOERASE), the RichEditCtrl doesn't display characters anymore and behaves erratically... did you even try this before saying it's better? – MasterMind Dec 06 '13 at 14:26