SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE | SIF_TRACKPOS;
richText.GetScrollInfo(SB_VERT, &si);
I'm using Visual C++ 7.0, .NET 1.0
Thanks.
I think perhaps the scrollbar for a rich edit is a separate control rather than a scrollbar in the NC area of the rich edit. Have
you tried GetScrollBarCtrl?
"Scot T Brennecke" wrote:
I tried using GetScrollBarCtrl, but it always returns NULL (for a
CRichEditCtrl). I tried using a CRichEditView instead, but it also returns
NULL when the style is a WS_VSCROLL or WS_HSCROLL. I'm not too familiar with
Window Styles, but my best guess is that I need to use a non-standard
scrollbar to not have WS_VSCROLL or WS_HSCROLL. Should I attach my own
scrollbar to the CRichEditView, or am I moving in the wrong direction?
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
I'm trying to make a program for monitoring data. The richtext field
displays various data, so I'm just trying to append text to the field (the
user isn't allowed to enter text in the field). I wrote a function called
AppendToRichText:
void AppendToRichText(CRichEditCtrl &richText, CHARFORMAT2 cf, std::string
str) {
int linePos;
int lineCnt;
CHARRANGE cursor;
SCROLLINFO si;
POINT p = {0, 0};
int winStatus = richText.IsWindowEnabled();
// Disable updating the window and any selection editing
richText.SetRedraw(FALSE);
richText.EnableWindow(FALSE);
// Back up the cursor and scroll bar position
richText.GetSel(cursor);
linePos = richText.LineFromChar(richText.LineIndex()) + 1;
lineCnt = richText.GetLineCount();
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS | SIF_RANGE | SIF_PAGE | SIF_TRACKPOS;
richText.GetScrollInfo(SB_VERT, &si);
// Move the cursor to the end of the window
richText.SetSel(-1, -1);
// Set up the character formatting
richText.SetSelectionCharFormat(cf);
// Append the text
richText.ReplaceSel(str.c_str());
// Restore the cursor and scroll bar position
// Autoscroll according to scrollbar pos - If nMax == nMin, then
// the scroll bar was grayed out
if (si.nTrackPos + (int)si.nPage >= si.nMax && si.nMin != si.nMax) {
// Increment the cursor if it was at the end of the window
if (linePos + 1 >= lineCnt) {
cursor.cpMin += richText.GetTextLength();
cursor.cpMax += richText.GetTextLength();
}
richText.SetSel(cursor);
UpdateScrollPos(richText, -1, BOTTOM);
}
else {
// Maintain the original cursor and scroll bar position
richText.SetSel(cursor);
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
p.y = si.nTrackPos;
richText.SetScrollInfo(SB_VERT, &si);
richText.SendMessage(EM_SETSCROLLPOS, 0, (LPARAM) &p);
}
// Re-Enable the window
richText.EnableWindow(winStatus);
richText.SetRedraw(winStatus);
richText.RedrawWindow();
}
NOTE: The function UpdateScrollPos will update the scroll bar to the bottom
of the richtext field - I want the control to automatically scroll as text is
appended if the scroll bar was previously at the bottom of the text field.
My problem right now is that when the AppendToRichText is called, if the
user is dragging on the scroll bar, the dragging status is lost and the
scroll bar is returned to its position before being dragged. It would also
be nice to keep the status of the user dragging the scroll bar.