richtextbox自动滚动的问题
自动滚动已经搞定。但现在的问题是,加载的文档越长,滚动越快。怎么让它无论加载的文档多长。都保持给定的速度。
private int min, max;
private int pos = 0;
private int endPos = 0;
private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private const int SB_THUMBPOSITION = 4;
[DllImport("user32.dll")]
private static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
private static extern int GetScrollPos(IntPtr hwnd, int nBar);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, int nBar, int wParam, int lParam);
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos);
private void rt_DoubleClick(object sender, EventArgs e)
{
timeAutoScroll.Interval = 100;
timeAutoScroll.Interval -= t.AutoScrollSpeed;
//得到滚动条的最大最小值
GetScrollRange(rt.Handle, SB_VERT, out min, out max);
//得到滚动条到最底下的实际位置
endPos = max - rt.ClientRectangle.Height;
this.timeAutoScroll.Enabled = true;
}
private void timeAutoScroll_Tick(object sender, EventArgs e)
{
pos = GetScrollPos(rt.Handle, SB_VERT);//加上这句是为了如果用户手动拖拽滚动条,可以保证滚动条继续从拖拽的位置走
pos++;
//如果已经到底,那么停止Timer
if (pos > endPos)
{
this.timeAutoScroll.Enabled = false;
return;
}
SetScrollPos(rt.Handle, SB_VERT, pos, true);
PostMessage(rt.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * pos, 0);
//PostMessage(rt.Handle, WM_VSCROLL, SB_THUMBPOSITION + pos*100000, 0);
}
------解决方案--------------------
protected override void WndProc(ref Message m)
{
int n = 20;//该值越大,滑轮滚动速度超快
case WM_MOUSEWHEEL:
中滚动,控制pos-=n;
}