Picturebox в форме - C#
Формулировка задачи:
в моей программе несколько pictureboxов можно перемещать при помощи мыши,как можно сделать так чтоб picturebox не выходил за приделы формы?
Решение задачи: «Picturebox в форме»
textual
Листинг программы
public partial class Form1 : Form
{
bool move;
Point current;
int border_w = 0; // Form border width
int border_h = 0; // Form border height (Title bar height)
public Form1()
{
InitializeComponent();
border_w = (Width - ClientSize.Width) / 2;
border_h = Height - ClientSize.Height - border_w;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
move = true;
current = new Point(e.X,e.Y);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (move)
{
Point newlocation = pictureBox1.Location;
newlocation.X += e.X - current.X;
newlocation.Y += e.Y - current.Y;
if (pictureBox1.Location.X < 0)
{
move = false;
newlocation.X = 0;
}
if (pictureBox1.Location.X > this.Width - pictureBox1.Width - border_w)
{
move = false;
newlocation.X = this.Width - pictureBox1.Width - border_w;
}
if (pictureBox1.Location.Y < 0)
{
move = false;
newlocation.Y = 0;
}
if (pictureBox1.Location.Y > this.Height - pictureBox1.Height - border_h)
{
move = false;
newlocation.Y = this.Height - pictureBox1.Height - border_h;
}
pictureBox1.Location = newlocation;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
move = false;
}
}