滚动面板时保持固定的控制位置

发布于 2024-08-15 12:30:19 字数 1164 浏览 5 评论 0原文

我在大型自定义面板 panel2 的中间左侧有一个标签 label1,该面板在父面板 panel1 中滚动。

替代文本 http://lh4.ggpht.com/_1TPOP7DzY1E/ szNN2g9Sv4I/AAAAAAAC1U/A_LlLOoejX8/s800/formScroll.png

我会标签1始终保持在面板2的可见左侧中间,即使在滚动时也是如此。

真实的例子,我的面板是一个用户控件,在其左侧生成一些标签。面板滚动,但我需要保持标签始终可见。

如何才能实现呢?

这是我的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
    }

    protected Point clickPosition;
    protected Point scrollPosition;

    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition = e.Location;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.SuspendLayout();
            this.scrollPosition += (Size)clickPosition - (Size)e.Location;
            this.panel1.AutoScrollPosition = scrollPosition;
            this.ResumeLayout(false);
        }
    }
}

I have a label label1 in the middle left of a large custom panel panel2 that is scrolled in a parent panel panel1.

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/SzNN2g9Sv4I/AAAAAAAAC1U/A_LlLOoejX8/s800/formScroll.png

I would keep the label1 always in the visible left middle of the panel2, even when scrolling.

In the real example, my panel is a user control that generates the some labels in its left side. The panel scrolls, but I need to keep the labels always visible.

How could it be achieved?

this is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
    }

    protected Point clickPosition;
    protected Point scrollPosition;

    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition = e.Location;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.SuspendLayout();
            this.scrollPosition += (Size)clickPosition - (Size)e.Location;
            this.panel1.AutoScrollPosition = scrollPosition;
            this.ResumeLayout(false);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两人的回忆 2024-08-22 12:30:19

嗯,技术上是可以的,你只需要在面板滚动时调整控制位置即可。例如:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      mPicturePos = pictureBox1.Location;
    }
    Point mPicturePos;
    private void panel1_Scroll(object sender, ScrollEventArgs e) {
      pictureBox1.Location = mPicturePos;
    }
  }

但是,您会看到当您滚动面板时控件将开始执行 pogo。这里的问题是 Windows 太有帮助了。它使用快速 bitblt 滚动窗口本身的内容,然后仅针对窗口中需要重新绘制的部分发送绘制请求。

它通过名为“拖动时显示窗口内容”的系统选项来执行此操作,该选项位于“控制面板”中“显示”小程序的“外观+效果”对话框中。您无法合理地关闭此选项,它会对系统范围产生影响。在Win7上它甚至不再暴露了。

除了一个简单的方法之外,没有好的解决方法:不要将控件放在面板中。只需确保它位于面板顶部即可。这在设计器中可能有点棘手,将其放在面板旁边(如有必要,将其置于前面)并手动编辑 Location 属性。

Well, it is technically possible, you just need to adjust the control position when the panel scrolls. For example:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      mPicturePos = pictureBox1.Location;
    }
    Point mPicturePos;
    private void panel1_Scroll(object sender, ScrollEventArgs e) {
      pictureBox1.Location = mPicturePos;
    }
  }

However, you'll see that the control will start doing the pogo when you scroll the panel. The problem here is that Windows is being too helpful. It scrolls the content of the window itself with a fast bitblt, then sends a paint request for only the parts of the window that needs to be repainted.

It does this directed by a system option named "Show window contents while dragging", available in Appearance + Effects dialog of the Display applet in Control Panel. You cannot reasonably turn this option off, it has system-wide effects. On Win7 it isn't even exposed anymore.

There is no good workaround for this, other than a simple one: don't put the control in the panel. Just make sure it is located on top of the panel. That can be a bit tricky in the designer, put it next to the panel (Bring To Front if necessary) and edit the Location property by hand.

小糖芽 2024-08-22 12:30:19

Hai serhio,

看看这个,它与你的问题有关..但我不知道它是否能解决你的问题,无论如何尝试维护表单中控件的大小和位置

和这个
维护树视图的滚动位置

Hai serhio,

Have a look at this it is related to ur question.. But i have no idea whether it would solve your prob anyhow give a try Maintaining the size and position of the Control in the form

and this one
Maintain scroll position of treeview

别忘他 2024-08-22 12:30:19
    public Form1()
    {
        this.InitializeComponent();
        panel2.Paint += new PaintEventHandler(panel2_Paint);
    }

    void panel2_Paint(object sender, PaintEventArgs e)
    {
        label1.Location = 
            new Point(-panel1.AutoScrollPosition.X, label1.Location.Y);
    }
    public Form1()
    {
        this.InitializeComponent();
        panel2.Paint += new PaintEventHandler(panel2_Paint);
    }

    void panel2_Paint(object sender, PaintEventArgs e)
    {
        label1.Location = 
            new Point(-panel1.AutoScrollPosition.X, label1.Location.Y);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文