我正在使用 SetScrollInfo API 在我的richtextbox(winforms)中滚动文本。我有一个计时器,每个刻度我需要滚动一个像素。一切都在较短的文本上完美运行。在较长的文本上,滚动方法滚动超过 1 个像素。我认为这与滚动条的有限部分有关。我怀疑最多有 65536 个零件,但也许我错了。调用 GetScrollInfo() 后 si.nMax = 65535 时出现问题
是否有任何解决方案可以在较长的文本上滚动一个像素?
// Scrolls a given textbox. handle: an handle to our textbox. pixels: number of pixels to scroll.
void scroll(IntPtr handle, int pixels)
{
mScrollsCounter++;
IntPtr ptrLparam = new IntPtr(0);
IntPtr ptrWparam;
// Get current scroller posion
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)ScrollInfoMask.SIF_ALL;
GetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si);
// Increase posion by pixles
if (si.nPos < (si.nMax - si.nPage))
si.nPos += pixels;
else
{
ptrWparam = new IntPtr(SB_ENDSCROLL);
t.Enabled = false;
SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
}
// Reposition scroller
SetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si, true);
// Send a WM_VSCROLL scroll message using SB_THUMBTRACK as wParam
// SB_THUMBTRACK: low-order word of wParam, si.nPos high-order word of wParam
ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * si.nPos);
SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
}
struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}