引入命名空间:
using System.Runtime.InteropServices;
声明Windows API:
[DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr SendMessage(IntPtr hwnd, Int32 wMsg, IntPtr wParam, IntPtr lParam);
定义常量:
enum ScrollBarMessage { WM_HSCROLL = 0x0114, // WM_HSCROLL消息 WM_VSCROLL = 0x0115 // WM_VSCROLL消息 }; enum HScrollBarCommands { SB_LINELEFT = 0, // 向左滚动一个单元 SB_LINERIGHT = 1, // 向右滚动一个单元 SB_PAGELEFT = 2, // 向左滚动一个窗口宽度 SB_PAGERIGHT = 3, // 向右滚动一个窗口宽度 SB_THUMBPOSITION = 4, SB_THUMBTRACK = 5, SB_LEFT = 6, // 滚动到最左边 SB_RIGHT = 7, // 滚动到最右边 SB_ENDSCROLL = 8 }; enum VScrollBarCommands { SB_LINEUP = 0, // 上滚一行 SB_LINEDOWN = 1, // 下滚一行 SB_PAGEUP = 2, // 上滚一页 SB_PAGEDOWN = 3, // 下滚一页 SB_THUMBPOSITION = 4, SB_THUMBTRACK = 5, SB_TOP = 6, // 滚动到顶部 SB_BOTTOM = 7, // 滚动到底部 SB_ENDSCROLL = 8 };
使用方法:
SendMessage( Control Handle, WM Scroll Message, (IntPtr) Scroll Command, IntPtr.Zero);
具体示例:
// 上滚一页 SendMessage(richTextBox1.Handle, (Int32)ScrollBarMessage.WM_VSCROLL, (IntPtr)VScrollBarCommands.SB_PAGEUP, IntPtr.Zero);
// 下滚一页 SendMessage(richTextBox1.Handle, (Int32)ScrollBarMessage.WM_VSCROLL, (IntPtr)VScrollBarCommands.SB_PAGEDOWN, IntPtr.Zero);