I have the following code performing an operation which scrolls a rich text box. However, what I am doing is not working very well. The text scrolls, but not consistently. Meaning, the amount of text being generated during the scroll is slowing down/speeding up (When less text is there) the scrolling speed. I am using a timer to control how fast the scroll bar should scroll.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
[DllImport("user32.dll")]
static extern int SetScrollInfo(IntPtr hwnd, int fnBar, [In] ref SCROLLINFO lpsi, bool fRedraw);
[DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
enum ScrollBarDirection
{
SB_HORZ = 0,
SB_VERT = 1,
SB_CTL = 2,
SB_BOTH = 3
}
enum ScrollInfoMask
{
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
SIF_POS = 0x4,
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = SIF_RANGE + SIF_PAGE + SIF_POS + SIF_TRACKPOS
}
const int WM_VSCROLL = 277;
const int SB_LINEUP = 0;
const int SB_LINEDOWN = 1;
const int SB_THUMBPOSITION = 4;
const int SB_THUMBTRACK = 5;
const int SB_TOP = 6;
const int SB_BOTTOM = 7;
const int SB_ENDSCROLL = 8;
Here is where the magic happens:
t.Interval = 30;
t.Tick += new EventHandler(t_Tick);
uptimer.Tick += new EventHandler(uptimer_Tick);
uptimer.Interval = speedslider.Value * 1000;
void uptimer_Tick(object sender, EventArgs e)
{
scroll(telep.Handle, (speedslider.Value));
}
private void scroll(IntPtr handle, int pixels)
{
{
IntPtr ptrLparam = new IntPtr(0);
IntPtr ptrWparam;
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)ScrollInfoMask.SIF_ALL;
GetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si);
{
if (si.nPos > (si.nMin))
si.nPos -= pixels;
else
{
ptrWparam = new IntPtr(SB_ENDSCROLL);
uptimer.Stop();
pictureBox5.Visible = true;
pictureBox1.Visible = true;
toolStripStatusLabel1.Text = "Teleprompter";
SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
}
}
SetScrollInfo(handle, (int)ScrollBarDirection.SB_VERT, ref si, true);
ptrWparam = new IntPtr(SB_THUMBTRACK + 0x10000 * si.nPos);
SendMessage(handle, WM_VSCROLL, ptrWparam, ptrLparam);
}
}
So basically I need help figuring out how to scroll a certain amount of pixels persecond. No more. no less. Because the amount of text that is on the next line slows down/speeds up the scrolling. Not sure why.
THank you, I have been puzzled over this for several days now.