简体   繁体   English

如何通过鼠标在面板内移动pictureBox

[英]How to move a pictureBox inside a Panel by Mouse

How to move a pictureBox inside a Panel by Mouse. 如何通过鼠标在Panel中移动pictureBox。 Visual Studio 2015 C# Winsows Forms Application. Visual Studio 2015 C#Winsows表单应用程序。

I've made a primitive slider to control the volume of my WindowsMediaPlayer. 我制作了一个基本的滑块来控制WindowsMediaPlayer的音量。 A panel as the background and a pictureBox inside as the slider-knopf. 一个面板作为背景,一个pictureBox作为滑块knopf。 And it works well. 而且效果很好。 But purely visually it does not work that good. 但是纯粹从视觉上看,效果并不理想。

I'v searched all around, but can't I find an answer to this little funny problem. 我到处搜寻,但是找不到这个有趣的小问题的答案。

Here is my code: 这是我的代码:

    int posY;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            posY = e.Y; ;
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        PictureBox box = sender as PictureBox;

        if (e.Button == MouseButtons.Left)
        {
            box.Top += e.Y - posY;
        }

        if (box.Top < 0)
        {
            box.Top = 0;
        }

        if (box.Top > 100)
        {
            box.Top = 100;
        }
        int n = box.Top;
        n = n * - 1 + 100;
        label1.Text = n.ToString();
    }

When I move the pictureBox out of the edge of the little panel, the pictureBox somehow 'shrinks' in the panel. 当将pictureBox移出小面板的边缘时,pictureBox会以某种方式在面板中“缩小”。 But when I release the mouse, the pictureBox restore its size. 但是当我释放鼠标时,pictureBox会恢复其大小。

Slider.gif Slider.gif

Why is that.? 这是为什么。? And how can I avoid it.? 我该如何避免呢?

Thanks. 谢谢。

I found a solution. 我找到了解决方案。 It's not optimal, but it can be used. 这不是最佳选择,但可以使用。 I changed the code to: 我将代码更改为:

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            dragging = true;
            startPoint = e.Location;
        }
    }


    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            Debug.WriteLine("mousemove X: " + e.X + " Y: " + e.Y);
            pictureBox1.Location = new Point(0, pictureBox1.Top + e.Location.Y - startPoint.Y);

            if (pictureBox1.Location.Y < 0)
            {
                pictureBox1.Location = new Point(0, 0);
                dragging = false;
            }
            if (pictureBox1.Location.Y > 100)
            {
                pictureBox1.Location = new Point(0, 100);
                dragging = false;
            }
            this.Refresh();
        }
        int n = pictureBox1.Location.Y;
        n = n * -1 + 100;
        label1.Text = n.ToString();

        mediaPlayer1.settings.volume = n;
    }


    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;
    }

gif gif

I still need to put an 'if' to correct, when the pictureBox1 is pulled out of the panel. 当pictureBox1从面板中拉出时,我仍然需要放置一个'if'来纠正。 And to avoid flicker, I have had to put a 'dragging = false'. 为了避免闪烁,我不得不输入“拖动=假”。 However, it results in the pictureBox1 is getting frozen to the edge, so I have to release the mouse, and re-click to continue. 但是,它导致pictureBox1被冻结到边缘,因此我必须释放鼠标,然后重新单击以继续。

But well - it's to live with. 但是-与它一起生活。

Thanks. 谢谢。

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

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