Discussion:
How to keep Horizontal Scroll in Multiline CEdit control
(too old to reply)
g***@firstlogic.com
2005-10-03 19:07:30 UTC
Permalink
When SetWindowText() is called, a multi-line CEdit control reverts to
being leftmost scrolled. Replacing the text with SetSel(-1,0) and then
ReplaceText() has the opposite effect of rightmost scrolling.

I needed the horizontal scrolling to stay extactly where it was.
CEdit::LineScroll() only offers a scroll position based on characters,
and there is no way to "get" the CEdit scrolling position by characters
in the first place.

After a pretty thorough search of Usenet postings, I did not find the
answer. After some research, I finally did figure out what to do and
thought I'd post it for others.

CEdit is derived from CWnd. The CWnd class holds the scroll bar and
has methods to access it. Here is a sample of code that worked for me
to keep the horizontal scrolling position steady. In this code,
m_ctlDisplay is a CEdit member variable of this dialog class. It has
been subclassed during dialog initialization.

int nScrollPos = 0;
...

// Get the scroll bar position.
nScrollPos = m_ctlDisplay.GetScrollPos(SB_HORZ);

// Set the new text in the address display window.
// This will reset the scroll bar and display scroll positions.
m_ctlDisplay.SetWindowText(sAddress);

// Reset the scroll bar position.
m_ctlDisplay.SetScrollPos(SB_HORZ, nScrollPos);

// Reset the display scroll position.
m_ctlDisplay.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBTRACK,
nScrollPos), 0);

That's all, I hope I saved someone some time.
r***@gmail.com
2014-05-14 18:10:56 UTC
Permalink
Post by g***@firstlogic.com
When SetWindowText() is called, a multi-line CEdit control reverts to
being leftmost scrolled. Replacing the text with SetSel(-1,0) and then
ReplaceText() has the opposite effect of rightmost scrolling.
I needed the horizontal scrolling to stay extactly where it was.
CEdit::LineScroll() only offers a scroll position based on characters,
and there is no way to "get" the CEdit scrolling position by characters
in the first place.
After a pretty thorough search of Usenet postings, I did not find the
answer. After some research, I finally did figure out what to do and
thought I'd post it for others.
CEdit is derived from CWnd. The CWnd class holds the scroll bar and
has methods to access it. Here is a sample of code that worked for me
to keep the horizontal scrolling position steady. In this code,
m_ctlDisplay is a CEdit member variable of this dialog class. It has
been subclassed during dialog initialization.
int nScrollPos = 0;
...
// Get the scroll bar position.
nScrollPos = m_ctlDisplay.GetScrollPos(SB_HORZ);
// Set the new text in the address display window.
// This will reset the scroll bar and display scroll positions.
m_ctlDisplay.SetWindowText(sAddress);
// Reset the scroll bar position.
m_ctlDisplay.SetScrollPos(SB_HORZ, nScrollPos);
// Reset the display scroll position.
m_ctlDisplay.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBTRACK,
nScrollPos), 0);
That's all, I hope I saved someone some time.
Thanks!
t***@gmail.com
2016-07-22 14:20:01 UTC
Permalink
Great - you saved me a lot of time. Thanks! :-)

Loading...