在 vb.net 中获取垂直滚动条位置作为整数
我有一个 RichTextBox,我需要找到垂直滚动条的位置。
没有 Pinvoke 有什么办法可以做到这一点吗?如果没有,有什么方法可以使用 Pinvoke 来做到这一点?
我需要返回一个整数值。
感谢您的帮助!
I have a RichTextBox and I need to find the position of the vertical scroll bar.
Is there any way to do this without Pinvoke? If not, what is a way to do this WITH Pinvoke?
I need to return an integer value.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,是的,Jay 的解决方案实现了承诺:它改变了滚动条的位置。它没有改变的是 RichTextBox 内容的位置。
这是我在 codeproject.com 找到的解决方案: https: //www.codeproject.com/Questions/293542/VB-Net-Custome-RichTextBox-SetScrollPos
Well, yeah, Jay's solution does what promised: it changes the positions of scrollbars. What it doesn't change is the position of the CONTENT of the richtextbox.
Here is the solution I found over at codeproject.com: https://www.codeproject.com/Questions/293542/VB-Net-Custome-RichTextBox-SetScrollPos
如果没有 PInvoke,我不知道有什么方法可以做到这一点。您可以使用 PInvoke 调用 GetScrollPos。
您可以尝试以下方法。
第 1 步:通过扩展标准 RichTextBox 创建自定义 RichTextBox 控件。
这将向标准 RichTextBox 添加两个属性:HScrollPos 和 VScrollPos。这些属性将允许您获取和设置控件中的水平和垂直滚动条位置。
第 2 步:创建一个测试表单并尝试您的控件。
在与自定义控件相同的项目中创建 Winform。将自定义控件拖放到测试窗体中,并在窗体上添加一个按钮。在窗体的 Click 事件中,添加以下代码以查看自定义控件的垂直滚动位置。
需要注意的一些事项:
如果您的控件当前未
显示垂直滚动条时,
调用 HScrollPos 会使你的
程序。有几个明显的
解决这个问题的方法(检查
之前显示过滚动条
检查财产,或作出
垂直滚动条始终可见等)。
取决于控制方式(以及
可能的形式)的大小(不
提及文本内容的变化
的控制),设置
VScrollPos 可能会使您的程序崩溃或
不将其恢复到原来的位置
当 VScrollPos 值为
保存。
我从来没有使用过这个代码。我
我以为你的问题是
有趣并且做了一点
研究得出我的答案。
I don't know any way to do this without PInvoke. You can use PInvoke to call GetScrollPos.
Here's how you can try it.
Step 1: Create a custom RichTextBox control by extending a standard RichTextBox.
This will add two properties to a standard RichTextBox: HScrollPos and VScrollPos. These properties will allow you go get and set the horizontal and vertical scrollbar position in your control.
Step 2: Create a test form and try out your control.
Create a Winform in the same project as your custom control. Drop the custom control in the test form and add a button on the form. In the form's Click event, add the following code to view your custom control's vertical scroll position.
A few things to watch for:
If your control is not currently
displaying a vertical scrollbar, the
call to HScrollPos will crash your
program. There are a few obviously
ways around this (check that the
scrollbar is being displayed before
checking the property, or making the
vertical scrollbar always visible, etc.).
Depending on how the control (and
possibly form) is sized (not to
mention changes in the text contents
of the control), setting the
VScrollPos can crash your program or
not restore it to the position it
was in when the VScrollPos value was
saved.
I've never used this code. I
thought your question was
interesting and did a little bit of
research to come up with my answer.