Search code examples
c++mfccricheditctrl

CRichEditCtrl prevent auto scroll on SetSel


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 ?


Solution

  • 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();
     }