禁用平滑滚动

时间:2018-03-21 12:28:26

标签: c# winforms scroll panel

我有一个Panel,其中使用了VerticalScroll。当用户单击VerticalScroll并拖动它时,VerticalScroll将平滑滚动。这是我不想要的。而不是平滑滚动,我希望VerticalScroll以不连续的步骤滚动,确切地说,与我在属性SmallChange中指定的步骤相同。我怎么能这样做?

编辑:它基本上似乎在覆盖Panel的OnPaint方法时有效,但是如何有条件地抑制对base.OnPaint的调用?

此代码将Vertical Scroll的位置设置为正确的值:

    protected override void OnPaint(PaintEventArgs e)
    {
        int v = this.VerticalScroll.Value;
        this.VerticalScroll.Value = Math.Max(0, Form1.AlignFileButtonHeight(v, v + 1));
    }

但它的效果是窗口瞬间“闪烁”!我怎么能避免这种情况?

编辑:到目前为止我取得的最好成绩,是通过覆盖OnScroll方法实现的:

    protected override void OnScroll(ScrollEventArgs se)
    {
        if (se.Type == ScrollEventType.ThumbTrack)
        {
            int v = se.NewValue;
            v = Math.Max(0, Form1.AlignFileButtonHeight(v, v + 1));
            this.VerticalScroll.Value = v;
            this.Refresh();
            se.NewValue = v;
            base.OnScroll(se);
            this.Refresh();
            base.OnScroll(se);
        }
    }

它仍然会闪烁一点,但不会像重写OnPaint方法那样多。但是,我想彻底摆脱眨眼。

编辑:我也试过这个,但成效非常有限。

    protected override void OnScroll(ScrollEventArgs se)
    {
        if (se.Type == ScrollEventType.ThumbTrack && se.NewValue != se.OldValue)
        {
            int diff = se.NewValue - se.OldValue;

            foreach (Control c in this.Controls)
            {
                int x = c.Location.X;
                int y = c.Location.Y;

                y = y + diff;

                c.Location = new Point(x, y);
            }

            if (se.NewValue % Form1.fileButtonHeight == 0)
            {
                foreach (Control c in this.Controls)
                {
                    int x = c.Location.X;
                    int y = c.Location.Y;

                    y = y + (diff < 0 ? 1 : -1) * Form1.fileButtonHeight;

                    c.Location = new Point(x, y);
                }
            }
        }
    }

0 个答案:

没有答案
相关问题