Re: CRichEditCtrl nTrackPos not working
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.
"Joseph M. Newcomer" wrote:
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
On Tue, 7 Jul 2009 16:04:01 -0700, ProtoMn <ProtoMn@discussions.microsoft.com> wrote:
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.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm