c# - 更改 TextBox 中的滚动条位置?

标签 c# .net c++ winforms scrollbar

如果我想改变TextBox的滚动条的位置,除了这个我还需要做什么:

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

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

最佳答案

首先,定义一个常量值:

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

关于c# - 更改 TextBox 中的滚动条位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4494162/

相关文章:

C++ 宏不正确地终止宏调用

c# - 在 C# 中实现 MS Word 打印质量

c# - 找不到 sendNotifications 选项 Google Calendar api v3 .NET

c# - Streamreader 和 StackOverflowException

.net - 带双括号 ()() 的 VB.NET 数组

c++ - while 循环没有正确终止

c++ - boost 哈纳 : create a map from a set and a default value

c# - 如何找出未调用 C# .NET 程序集中的哪些代码

c# - 如何创建可用的 URL 来打开文档?

c# - C# 中的 Stream Empty 读取并上传 Excel 文件