How to move a pictureBox inside a Panel by Mouse. Visual Studio 2015 C# Winsows Forms Application.
如何通过鼠标在Panel中移动pictureBox。 Visual Studio 2015 C#Winsows Forms应用程序。
I've made a primitive slider to control the volume of my WindowsMediaPlayer. A panel as the background and a pictureBox inside as the slider-knopf. And it works well. But purely visually it does not work that good.
我制作了一个原始的滑块来控制我的WindowsMediaPlayer的音量。一个面板作为背景,一个pictureBox作为slider-knopf。它运作良好。但纯粹在视觉上它并没有那么好用。
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. But when I release the mouse, the pictureBox restore its size.
当我将pictureBox从小面板的边缘移开时,pictureBox在面板中以某种方式“收缩”。但是当我释放鼠标时,pictureBox恢复了它的大小。
Slider.gif
Why is that.? And how can I avoid it.?
这是为什么。?我怎么能避免它。
Thanks.
谢谢。
1 个解决方案
#1
0
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
I still need to put an 'if' to correct, when the pictureBox1 is pulled out of the panel. 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被拉出面板时,我仍然需要设置'if'来纠正。为了避免闪烁,我不得不设置'拖动=假'。但是,它导致pictureBox1被冻结到边缘,所以我必须释放鼠标,然后重新单击继续。
But well - it's to live with.
但是好吧 - 这就是生活。
Thanks.
谢谢。
#1
0
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
I still need to put an 'if' to correct, when the pictureBox1 is pulled out of the panel. 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被拉出面板时,我仍然需要设置'if'来纠正。为了避免闪烁,我不得不设置'拖动=假'。但是,它导致pictureBox1被冻结到边缘,所以我必须释放鼠标,然后重新单击继续。
But well - it's to live with.
但是好吧 - 这就是生活。
Thanks.
谢谢。