【发布时间】:2018-04-03 16:54:15
【问题描述】:
我做了一个同步两个 RichTextBox 滚动的代码。 希望这与行号无关。
但是当 RichTextBox 的行变大时(大约 2000+),System.OverflowException 发生在 SendMessage 方法。
用 try/catch 覆盖 SendMessage 并不能使其工作。
有什么方法可以处理大于 Int.MaxValue 的 IntPtr?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int a = 0; a < 4000; a++)
{
RTB1.Text += a + "\n";
RTB2.Text += a + "\n";
}
}
[DllImport("User32.dll")]
public extern static int GetScrollPos(IntPtr hWnd, int nBar);
[DllImport("User32.dll")]
public extern static int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
private void RTB1_VScroll(object sender, EventArgs e)
{
int nPos = GetScrollPos(RTB1.Handle, (int)ScrollBarType.SbVert);
nPos <<= 16;
uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
SendMessage(RTB2.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0)); //Error occurs here.
}
public enum ScrollBarType : uint
{
SbHorz = 0,
SbVert = 1,
SbCtl = 2,
SbBoth = 3
}
public enum Message : uint
{
WM_VSCROLL = 0x0115
}
public enum ScrollBarCommands : uint
{
SB_THUMBPOSITION = 4
}
}
【问题讨论】:
-
WM_VSCROLL 的MSDN article 专门讨论了“16 位屏障”以及您如何处理它。请改用 SetScrollInfo()。