面板中的移动图片框

发布于 2024-08-16 03:35:14 字数 297 浏览 8 评论 0原文

我有一个 C#、WindowsForms 项目,我创建了一个 panel,其中包含一个 pictureBox,它比他的父级大得多。

我将 panel.AutoScroll 设置为 true,我想要做的是将这个 pictureBox 拖到 panel 中,而不是捕获一个卷轴并移动它。

即,当我抓取图像并将光标向左和向下移动时,我希望获得与使用面板滚动条相同的行为。

怎么办呢?

I have a project in C#, WindowsForms and I created a panel that contains a pictureBox that is much bigger than his parent.

I turned panel.AutoScroll to true and what I want to do is dragging this pictureBox in panel instead of catching a scroll and moving it.

I.e. when I grab an image and move cursor to left and down I would like to get the same behavior as I will do it with panel's scrolls.

How to do it ?

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

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

发布评论

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

评论(3

风吹短裙飘 2024-08-23 03:35:14

好的,我明白了。 ;-) 如果其他人也有同样的问题,这里是解决方案:

    protected Point clickPosition;
    protected Point scrollPosition; 

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition.X = e.X;
        this.clickPosition.Y = e.Y;
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            scrollPosition.X = scrollPosition.X + clickPosition.X - e.X;
            scrollPosition.Y = scrollPosition.Y + clickPosition.Y - e.Y;
            this.panel.AutoScrollPosition = scrollPosition;
        }
    }  

Ok, I got it. ;-) If anyone else has the same problem, here is solution:

    protected Point clickPosition;
    protected Point scrollPosition; 

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition.X = e.X;
        this.clickPosition.Y = e.Y;
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            scrollPosition.X = scrollPosition.X + clickPosition.X - e.X;
            scrollPosition.Y = scrollPosition.Y + clickPosition.Y - e.Y;
            this.panel.AutoScrollPosition = scrollPosition;
        }
    }  
樱花落人离去 2024-08-23 03:35:14

hsz 解决方案的较小变体:)

    protected Point clickPosition;
    protected Point scrollPosition;

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

    private void pictureBox1_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);
        }
    }

a smaller variant of the hsz solution :)

    protected Point clickPosition;
    protected Point scrollPosition;

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

    private void pictureBox1_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);
        }
    }
天冷不及心凉 2024-08-23 03:35:14

hsz' 的改进解决方案,具有滚动限制,但我只允许垂直滚动

protected Point clickPosition;
protected Point scrollPosition;

private void picBoxScan_MouseDown(object sender, MouseEventArgs e)
{
    this.clickPosition.X = e.X;
    this.clickPosition.Y = e.Y;
}

private void picBoxScan_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        scrollPosition.X = panelViewFile.AutoScrollPosition.X;
        scrollPosition.Y = scrollPosition.Y + (clickPosition.Y - e.Y);
        scrollPosition.Y = Math.Min(scrollPosition.Y,panelViewFile.VerticalScroll.Maximum);
        scrollPosition.Y = Math.Max(scrollPosition.Y,panelViewFile.VerticalScroll.Minimum);
        panelViewFile.AutoScrollPosition = scrollPosition;
    }
}

an improved solution from hsz', with limitation of scroll, but I allow only vertical scroll

protected Point clickPosition;
protected Point scrollPosition;

private void picBoxScan_MouseDown(object sender, MouseEventArgs e)
{
    this.clickPosition.X = e.X;
    this.clickPosition.Y = e.Y;
}

private void picBoxScan_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        scrollPosition.X = panelViewFile.AutoScrollPosition.X;
        scrollPosition.Y = scrollPosition.Y + (clickPosition.Y - e.Y);
        scrollPosition.Y = Math.Min(scrollPosition.Y,panelViewFile.VerticalScroll.Maximum);
        scrollPosition.Y = Math.Max(scrollPosition.Y,panelViewFile.VerticalScroll.Minimum);
        panelViewFile.AutoScrollPosition = scrollPosition;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文