Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CRichEditCtrl nTrackPos not working

148 views
Skip to first unread message

ProtoMn

unread,
Jul 7, 2009, 7:04:01 PM7/7/09
to
Does anybody know why when calling GetScrollInfo for a CRichEditCtrl, my nPos
would be the exact same as my nTrackPos even though I've been dragging the
scrollbar? I set up a break point to hit when I'm clicking and dragging the
vertical scroll bar and the nTrackPos is the same as nPos, which is the
position of the scroll bar before I began dragging the scroll bar. Here is
the code I am running before my break point:

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.

Scot T Brennecke

unread,
Jul 8, 2009, 12:27:57 AM7/8/09
to

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?

ProtoMn

unread,
Jul 8, 2009, 12:26:02 PM7/8/09
to


"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

unread,
Jul 11, 2009, 6:39:01 PM7/11/09
to
The code shown below is meaningless. You have shown it out of context. Without the
context in which it is executed, there is no way to tell what its purpose is, or what
conditions are involved in its execution.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

ProtoMn

unread,
Jul 13, 2009, 11:08:00 AM7/13/09
to
Sorry, I didn't feel that information was relevant. That could easily be my
problem, I'm fairly new to MFC and don't know all that is required for the
setup process.

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.

0 new messages