MasterMind
MasterMind

Reputation: 389

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 ?

Upvotes: 3

Views: 2070

Answers (3)

Gautam Jain
Gautam Jain

Reputation: 6849

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);

Upvotes: 0

user3073563
user3073563

Reputation: 1

Change to

RedrawWindow(0,0,RDW_NOERASE);

It's better.

Upvotes: -2

MasterMind
MasterMind

Reputation: 389

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

Upvotes: 3

Related Questions