Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.


C#
[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:

C#
t.Interval = 30;
            t.Tick += new EventHandler(t_Tick);
            uptimer.Tick += new EventHandler(uptimer_Tick);
            uptimer.Interval = speedslider.Value * 1000;//speedslider is just a trackbar

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.
Posted
Updated 7-Jun-12 8:54am
v2

1 solution

I have found the answer to my question. I simply needed to change my timer interval to 1 and then change my scrolling number of pixels to my speedslider value:


C#
toolStripStatusLabel1.Text = "Scrolling down";
            uptimer.Stop();
            downtimer.Start();
            downtimer.Interval = 1;
            downtimer.Tick += new EventHandler(downtimer_Tick);

            scrolld(telep.Handle, speedslider.Value);



This allows me to scroll the scroll bar by 1, and only, 1 pixel per millisecond. It will limit the speed of the rendering so that the application is controlled rather than going "all-out". And if the user wants a faster scroll. Rather than scrolling faster, more pixels are scrolled in the same amount of time. I think I have done a good job here. However I just realised that the more text in the text box. The more text will be scrolled. This is because with more 'stuff' inside my text box, one pixel of scrolling will cover more of this 'stuff' therefore, if I paste War and Peace, the scrolling is much faster than a 200 character speech. Anyone have an idea of how to fix this? Thanks
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900