禁用 RichTextBox 自动滚动
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试 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
如果您的问题是在添加日志文本时“垂直滚动”向下滚动,但您希望它始终位于顶部:
您必须向 VScroll、TextChanged 事件以及事件处理程序集中添加事件处理程序滚动到顶部
您也可以对水平滚动条执行相同的操作。将 WM_VSCROLL 替换为 WM_HSCROLL,将 SB_TOP 替换为 SB_LEFT
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
You could do the same with horizontal scroll bar too. Replace WM_VSCROLL with WM_HSCROLL and SB_TOP with SB_LEFT