禁用 RichTextBox 自动滚动

发布于 2024-10-16 18:13:24 字数 325 浏览 5 评论 0原文

我正在使用 RichTextBox 控件来显示应用程序日志。我通过几次 RichTextBox::AppendText 方法的调用每秒更新一次控件。对我来说真正烦人的是光标不断滚动到文本的最后一行。当用户需要分析开头的日志时,这是非常不舒服的。我已尝试以下解决方案来解决我的问题:

int pos = tb_logs.SelectionStart;
tb_logs.AppendText("log message");
tb_logs.SelectionStart = pos;

这并没有触及问题的核心,因为控件会定期重绘,这非常分散注意力。有一些更清洁的解决方案吗?

I am using RichTextBox control for displaying application logs. I am updating control once a second with a few calls of RichTextBox::AppendText method. What is really annoying for me is that cursor keeps scrolling to the last line of text. Its very uncomfortable in situation when user needs to analyze logs that are at the beginning. I have tried following solution to my problem:

int pos = tb_logs.SelectionStart;
tb_logs.AppendText("log message");
tb_logs.SelectionStart = pos;

This does not go to the core of problem because control is being periodically redrawed which is very distracting. Is there some cleaner solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-10-23 18:13:25

您可以尝试 tb_logs.SelectionLength = 1;与 SelectionStart 一起。这将从您当前的位置中选择 1 个角色。

没试过...但可能有用

You may try tb_logs.SelectionLength = 1; along with SelectionStart. This will make 1 char selected from your Current Position.

Not Tried it...But may work

深海蓝天 2024-10-23 18:13:24

如果您的问题是在添加日志文本时“垂直滚动”向下滚动,但您希望它始终位于顶部:

您必须向 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;

If your issue is with the "Vertical Scroll" scrolling down when you are adding the Log text, but you would want it to be on top all the time:

you have to add event handlers to VScroll, TextChanged events and in the event handler set the scroll to top

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);
}

You could do the same with horizontal scroll bar too. Replace WM_VSCROLL with WM_HSCROLL and SB_TOP with SB_LEFT

private const UInt32 WM_HSCROLL = 0x0114;
private const UInt32 SB_LEFT = 0x06;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文