更改文本框中滚动条的位置?

发布于 2024-10-08 09:20:54 字数 266 浏览 6 评论 0原文

如果我想更改 TextBox 滚动条的位置,除此之外我还需要做什么:

SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

此函数仅更改滚动条位置,但不会更新实际的 TextBox (因此滚动条“滚动”,但文本不滚动)。有什么建议吗?我正在使用 Windows 窗体和 .NET 4 以及 Visual Studio 2008。

If I want to change the position of a TextBox's scrollbar, what do I need to do besides this:

SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

This function only changes the scrollbar position, but it doesn't update the actual TextBox (so the scrollbar "scrolls", but the text doesn't). Any suggestions? I'm using Windows Forms and .NET 4, with Visual Studio 2008.

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

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

发布评论

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

评论(3

巾帼英雄 2024-10-15 09:20:54

我通常这样做:

textBox1.Select(textBox1.Text.Length, 0);
textBox1.ScrollToCaret();

选择 0 个字符只是将光标移动到所需位置(在示例代码中:在文本末尾)。

I usually do:

textBox1.Select(textBox1.Text.Length, 0);
textBox1.ScrollToCaret();

Where selecting 0 characters just moves the cursor to the desired location (in the example code: at the end of the text).

沉鱼一梦 2024-10-15 09:20:54

首先,定义一个常量值:

const int EM_LINESCROLL = 0x00B6;

然后,声明 user32.dll 的两个外部方法:

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, 
                               int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, 
                               int wParam, int lParam);

最后,使用这些方法来做实际的事情:

SetScrollPos(myTextBox.Handle,1,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,
                             myTextBox.Lines.Length-1);

您还可以使用 GetScrollPos()用于在文本框更新时保存滚动位置:

[DllImport("user32.dll")]
static extern int GetScrollPos(IntPtr hWnd, int nBar);

First, define a constant value:

const int EM_LINESCROLL = 0x00B6;

Then, declare two external methods of user32.dll:

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, 
                               int nPos, bool bRedraw);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int wMsg, 
                               int wParam, int lParam);

Finally, use these methods to do the real thing:

SetScrollPos(myTextBox.Handle,1,myTextBox.Lines.Length-1,true);
SendMessage(myTextBox.Handle,EM_LINESCROLL,0,
                             myTextBox.Lines.Length-1);

You may also use GetScrollPos() for scroll position saving when textbox is updated:

[DllImport("user32.dll")]
static extern int GetScrollPos(IntPtr hWnd, int nBar);
孤城病女 2024-10-15 09:20:54

请尽量避免直接控制它,它的效果并不好。设置 TextBox.SelectionStart 属性以确保插入符号是您想要使其可见的行。然后调用ScrollToCaret。控件必须具有焦点才能使其工作。您的用户找回它不会有任何问题。

TextBox 是控件鼻祖的包装器,它已经有 23 岁了,我估计比许多 SO 用户都老。那时 640 KB 对于每个人来说都足够了,Windows 2.0 必须在 386SUX 或更少的操作系统上运行。 WPF版本有更多的口哨。

Do try to avoid controlling this directly, it just doesn't work really well. Set the TextBox.SelectionStart property to ensure the caret is the line you want to make visible. Then call ScrollToCaret. The control must have the focus to make this work. Your user won't have any trouble finding it back.

TextBox is a wrapper for the grand-daddy of controls, it's 23 years old already, older than many SO users I reckon. Back when 640 KB was enough for everybody and Window 2.0 had to run on a 386SUX or less. The WPF version has more whistles.

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