如果您的问题是在添加日志文本时“垂直滚动”向下滚动,但您希望它始终位于顶部:
您必须将事件处理程序添加到 VScroll、TextChanged 事件并在事件处理程序中将滚动设置为顶部
richTextBox1.VScroll += HandleRichTextBoxAdjustScroll;
richTextBox1.TextChanged += HandleRichTextBoxAdjustScroll;
private const UInt32 SB_TOP = 0x6;
private const UInt32 WM_VSCROLL = 0x115;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, UInt32 Msg,
IntPtr wParam, IntPtr lParam);
private void HandleRichTextBoxAdjustScroll(Object sender,
EventArgs e)
{
PostMessage(handle, WM_VSCROLL, (IntPtr)SB_TOP, IntPtr.Zero);
}
你也可以对水平滚动条做同样的事情。将 WM_VSCROLL 替换为 WM_HSCROLL 并将 SB_TOP 替换为 SB_LEFT
private const UInt32 WM_HSCROLL = 0x0114;
private const UInt32 SB_LEFT = 0x06;