c# 在 Panel 中拖动 PictureBox
c# Dragging PictureBox inside Panel
我有一个 PictureBox1 可使用此代码拖动:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + pictureBox1.Width> panel1.Width)
pictureBox1.Left += (e.X - x);
}
}
但是我无法获取绑定限制,
我只想在面板内移动图片,如下所示:
Example
有什么想法吗?
谢谢
试试这个:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int left = 0;
if (pictureBox1.Left >= 0 && pictureBox1.Right <= panel1.Width)
left = pictureBox1.Left + (e.X - x);
left = Math.Min(panel1.Width - pictureBox1.Width , left);
left = Math.Max(0, left);
pictureBox1.Left = left;
}
}
我有一个 PictureBox1 可使用此代码拖动:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + pictureBox1.Width> panel1.Width)
pictureBox1.Left += (e.X - x);
}
}
但是我无法获取绑定限制, 我只想在面板内移动图片,如下所示: Example
有什么想法吗? 谢谢
试试这个:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int left = 0;
if (pictureBox1.Left >= 0 && pictureBox1.Right <= panel1.Width)
left = pictureBox1.Left + (e.X - x);
left = Math.Min(panel1.Width - pictureBox1.Width , left);
left = Math.Max(0, left);
pictureBox1.Left = left;
}
}