【问题标题】:Get real-time scrolling without AutoScroll无需 AutoScroll 即可获得实时滚动
【发布时间】:2013-10-14 21:03:20
【问题描述】:

在问完这个问题后:Prevent AutoScroll when contained Control gets focus 我找到了一种没有 AutoScroll 的滚动条(使用可以访问 VScroll 属性的派生类)。但是 - 它不是实时的。即只有当用户完成滚动时,控件才会真正滚动。 (与带有AutoScroll = true 的面板相反。)那么我如何让它实时滚动呢?

我的代码:

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; }
    }
}

【问题讨论】:

  • 您需要覆盖面板的 OnScroll() 方法并调用 SetDisplayRectLocation(0, -se.NewValue)。这会使保修失效。
  • @HansPassant 再次感谢。效果很好。您可以将其转换为答案。它值得代表。
  • @HansPassant This voids the warranty. - 你的意思是这样做有问题吗?

标签: c# .net winforms


【解决方案1】:

如评论:

您需要覆盖面板的 OnScroll() 方法并调用 SetDisplayRectLocation(0, -se.NewValue)。

这就是这个问题的答案。

但是,我发现我不能同时拥有两个滚动条。或者至少 - 我还没有找到办法。

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多