简体   繁体   English

无需AutoScroll即可进行实时滚动

[英]Get real-time scrolling without AutoScroll

After asking this: Prevent AutoScroll when contained Control gets focus I've found a way to have a scrollbar without the AutoScroll (using a derived class which can access the VScroll property). 此问题之后: 防止包含控件获得焦点时自动滚动我找到了一种不用自动滚动的滚动条的方法(使用可以访问VScroll属性的派生类)。 However - it's not in real-time. 但是-它不是实时的。 ie Only when the user is done scrolling does the Control actually get scrolled. 即只有当用户完成滚动时,控件才真正滚动。 (as opposed to a Panel with AutoScroll = true .) So how do I get it to scroll in real-time? (与使用AutoScroll = true的Panel相对)。那么如何使它实时滚动?

My code: 我的代码:

using System.Drawing;
using System.Windows.Forms;

namespace test
{
    public partial class Form1 : Form
    {
        MyPanel panel = new MyPanel
        {
            //AutoScroll = true,
            Size = new Size(200, 200),
            Location = new Point(0, 30),
            BackColor = Color.Green
        };
        Button b1 = new Button
        {
            Location = new Point(100, 100),
            Size = new Size(50, 150),
            BackColor = Color.Black
        };
        Button b2 = new Button();

        public Form1()
        {
            InitializeComponent();
            panel.Controls.Add(b1);
            Controls.Add(panel);
            Controls.Add(b2);
            Shown += new System.EventHandler(Form1_Shown);
        }

        void Form1_Shown(object sender, System.EventArgs e)
        {
            panel.VerticalScroll.Visible = true;
            panel.SetV();
        }
    }

    class MyPanel : Panel
    {
        public void SetV() { VScroll = true; }
    }
}

As in the comment: 如评论中所示:

You'll need to override the panel's OnScroll() method and call SetDisplayRectLocation(0, -se.NewValue). 您需要覆盖面板的OnScroll()方法,并调用SetDisplayRectLocation(0,-se.NewValue)。

That's the answer to this question. 这就是这个问题的答案。

However, I've found that I can't have both scrollbars simultaneously. 但是,我发现我不能同时拥有两个滚动条。 Or at least - I haven't found a way to do it. 或者至少-我还没有找到一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM