简体   繁体   English

如何将滚动条向上移动一行? (在C#RichTextBox中)

[英]How to move scroll bar up by one line? (In C# RichTextBox)

For my C# RichTextBox, I want to programmatically do the same thing as clicking the up arrow at the top of a vertical scroll bar, which moves the RichTextBox display up by one line. 对于我的C#RichTextBox,我想以编程方式执行与单击垂直滚动条顶部的向上箭头相同的操作,这会将RichTextBox显示向上移动一行。 What is the code for this? 这是什么代码? Thanks! 谢谢!

Here's what I do: 这是我做的:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, 
                               UIntPtr wParam, IntPtr lParam);

then call: 然后打电话:

SendMessage(myRichTextBox.Handle, (uint)0x00B6, (UIntPtr)0, (IntPtr)(-1));

Seems to work OK - you might need to tweak things a bit, though. 似乎工作正常 - 但你可能需要稍微调整一下。

Hope that helps. 希望有所帮助。

For future reference the EM_LINESCROLL message is what you send to any multi-line edit control to set the scroll position. 为了将来参考,EM_LINESCROLL消息是您发送到任何多行编辑控件以设置滚动位置的消息。 You can scroll vertically or horizontally. 您可以垂直或水平滚动。 See MSDN for details. 有关详细信息,请参阅MSDN

You can also use the Rich Edit Selection method, where you set the character index (which you can get with EM_LINEINDEX) then call RichEdit.ScrollToCaret ie: 您还可以使用Rich Edit Selection方法设置字符索引(可以使用EM_LINEINDEX获得),然后调用RichEdit.ScrollToCaret,即:

RichEdit.SelectionStart = SendMessage(RichEdit.Handle, EM_LINEINDEX, ScrollTo, 0);
RichEdit.ScrollToCaret();

This will scroll that line to the top of the edit control. 这会将该行滚动到编辑控件的顶部。

window.scrollBy(0,20); window.scrollBy(0,20);

This will scroll the window. 这将滚动窗口。 20 is an approximate value I have used in the past that typically equals one line... but of course font size may impact how far one line really is. 20是我过去使用的近似值,通常等于一行...但当然字体大小可能会影响一行的实际距离。

如果您可以获得富文本框的滚动控件,您应该能够获取其SmallChange属性并使用它来滚动文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM