//pBox
pictureBoxVideo[0] = pictureBox1;
pictureBoxVideo[1] = pictureBox2;
pictureBoxVideo[2] = pictureBox3;
pictureBoxVideo[3] = pictureBox4;
pictureBoxVideo[4] = pictureBox5;
pictureBoxVideo[5] = pictureBox6;
pictureBoxVideo[6] = pictureBox7;
pictureBoxVideo[7] = pictureBox8;
//cBox
controlCloneVideo[0] = controlClone1;
controlCloneVideo[1] = controlClone2;
controlCloneVideo[2] = controlClone3;
controlCloneVideo[3] = controlClone4;
controlCloneVideo[4] = controlClone5;
controlCloneVideo[5] = controlClone6;
controlCloneVideo[6] = controlClone7;
controlCloneVideo[7] = controlClone8;
前提(cBox默认只显示四个画面,这四个画面用于显示bBox拖进的画面,cBox是对pBox的克隆)
1.cBox[i]对应显示pBox[i]的画面(如c[0]显示p[0],c[1]显示p[1],...相当于一对一克隆)
2.将pBox[i]随意一个拖动到cBox[0]任意一个控件中,将pBox[0]的画面变为1像素,c[0]显示p[i],的画面
3.当第三次将pBox[i]拖到cBox[0]时,将上一个拖进的画面变为1像素,将此画面显示在p[0]上(无限循环)
实现步骤:
1.获取鼠标在窗口中的坐标 cursor.position
2.判断鼠标表在那个窗口里(利用窗口的大小和范围判断)
if (((s.X <= p.X) && (p.X <= (s.X + controlCloneVideo[i].Width))) && ((s.Y <= p.Y) && (p.Y <= (s.Y + controlCloneVideo[i].Height)))){}
3.将窗口交换
4.优化代码(将所有的pictureBox的mouse事件只想已写的mouse事件(如mouse3))
this.pictureBox7.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseDown);
this.pictureBox7.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseMove);
this.pictureBox7.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseUp);
具体代码如下:
private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
{
isDrag = false;
snapShotPicture.Visible = false;
Point p = new Point(); //鼠标位置
p = Cursor.Position;
Point s = new Point(); //鼠标所在控件位置
int a = Convert.ToInt32((sender as PictureBox).Tag) - 1; //获取当前控件的Tag值(需要注意的地方Tag的值是从1开始,而数组是从0开始)
for (int i = 0; i < 8; i++)
{
//int tag = Convert.ToInt32(pictureBoxVideo[i].Tag) - 1;
s.X = controlCloneVideo[i].Left;
s.Y = controlCloneVideo[i].Top;
s = controlCloneVideo[i].PointToScreen(new Point(0, 0));
if (((s.X <= p.X) && (p.X <= (s.X + controlCloneVideo[i].Width))) && ((s.Y <= p.Y) && (p.Y <= (s.Y + controlCloneVideo[i].Height))))
{
//将cloneVideo[i]与cloneVideo[a]位置交换
int x1 = controlCloneVideo[i].Left;
int y1 = controlCloneVideo[i].Top;
int height1 = controlCloneVideo[i].Height;
int width1 = controlCloneVideo[i].Width;
//controlCloneVideo[i].Height = 1;
//controlCloneVideo[i].Width = 1;
//controlCloneVideo[i].Left = -1;
//controlCloneVideo[i].Top = -1;
int x2 = controlCloneVideo[a].Left;
int y2 = controlCloneVideo[a].Top;
int height2 = controlCloneVideo[a].Height;
int width2 = controlCloneVideo[a].Width;
controlCloneVideo[i].Height = height2;
controlCloneVideo[i].Width = width2;
controlCloneVideo[i].Left = x2;
controlCloneVideo[i].Top = y2;
controlCloneVideo[a].Top = y1;
controlCloneVideo[a].Left = x1;
controlCloneVideo[a].Width = width1;
controlCloneVideo[a].Height = height1;
controlCloneVideo[a].Visible = true; //需要注意的地方(容易疏漏)
return;
}
}
}
private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
{
if (isDrag)
{
int n = Convert.ToInt32((sender as PictureBox).Tag) - 1;
for (int i = 0; i < 8; i++)
{
if (n == i)
{
snapShotPicture.Left += pictureBoxVideo[i].Left + Convert.ToInt16(e.X - snapShotPicture.Left);
snapShotPicture.Top += pictureBoxVideo[i].Top + Convert.ToInt16(e.Y - snapShotPicture.Top);
return;
}
}
}
}
private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
{
isDrag = true;
#region
// snapShotPicture.Left = e.X;
// snapShotPicture.Top = e.Y;
//截图
// Bitmap Pic = new Bitmap(138, 139);
// Graphics g = Graphics.FromImage(Pic);
//Bitmap Pic = new Bitmap(pictureBoxVideo[2].Width, pictureBoxVideo[2].Height);
//Graphics g = Graphics.FromImage(Pic);
//snapShotPicture.Left = pictureBoxVideo[2].Left;
//snapShotPicture.Top = pictureBoxVideo[2].Top;
//g.CopyFromScreen(pictureBoxVideo[2].PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(pictureBoxVideo[2].Width, pictureBoxVideo[2].Height));
//snapShotPicture.Image = Pic;
//snapShotPicture.Visible = true;
#endregion
int s = Convert.ToInt32((sender as PictureBox).Tag) - 1;
for (int i = 0; i < 8; i++)
{
if (s == i)
{
Bitmap Pic = new Bitmap(pictureBoxVideo[i].Width, pictureBoxVideo[i].Height);
Graphics g = Graphics.FromImage(Pic);
snapShotPicture.Left = pictureBoxVideo[s].Left;
snapShotPicture.Top = pictureBoxVideo[s].Top;
g.CopyFromScreen(pictureBoxVideo[s].PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(pictureBoxVideo[s].Width, pictureBoxVideo[s].Height));
snapShotPicture.Image = Pic;
snapShotPicture.Visible = true;
return;
}
}
}
}
pictureBoxVideo[0] = pictureBox1;
pictureBoxVideo[1] = pictureBox2;
pictureBoxVideo[2] = pictureBox3;
pictureBoxVideo[3] = pictureBox4;
pictureBoxVideo[4] = pictureBox5;
pictureBoxVideo[5] = pictureBox6;
pictureBoxVideo[6] = pictureBox7;
pictureBoxVideo[7] = pictureBox8;
//cBox
controlCloneVideo[0] = controlClone1;
controlCloneVideo[1] = controlClone2;
controlCloneVideo[2] = controlClone3;
controlCloneVideo[3] = controlClone4;
controlCloneVideo[4] = controlClone5;
controlCloneVideo[5] = controlClone6;
controlCloneVideo[6] = controlClone7;
controlCloneVideo[7] = controlClone8;
前提(cBox默认只显示四个画面,这四个画面用于显示bBox拖进的画面,cBox是对pBox的克隆)
1.cBox[i]对应显示pBox[i]的画面(如c[0]显示p[0],c[1]显示p[1],...相当于一对一克隆)
2.将pBox[i]随意一个拖动到cBox[0]任意一个控件中,将pBox[0]的画面变为1像素,c[0]显示p[i],的画面
3.当第三次将pBox[i]拖到cBox[0]时,将上一个拖进的画面变为1像素,将此画面显示在p[0]上(无限循环)
实现步骤:
1.获取鼠标在窗口中的坐标 cursor.position
2.判断鼠标表在那个窗口里(利用窗口的大小和范围判断)
if (((s.X <= p.X) && (p.X <= (s.X + controlCloneVideo[i].Width))) && ((s.Y <= p.Y) && (p.Y <= (s.Y + controlCloneVideo[i].Height)))){}
3.将窗口交换
4.优化代码(将所有的pictureBox的mouse事件只想已写的mouse事件(如mouse3))
this.pictureBox7.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseDown);
this.pictureBox7.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseMove);
this.pictureBox7.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox3_MouseUp);
具体代码如下:
private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
{
isDrag = false;
snapShotPicture.Visible = false;
Point p = new Point(); //鼠标位置
p = Cursor.Position;
Point s = new Point(); //鼠标所在控件位置
int a = Convert.ToInt32((sender as PictureBox).Tag) - 1; //获取当前控件的Tag值(需要注意的地方Tag的值是从1开始,而数组是从0开始)
for (int i = 0; i < 8; i++)
{
//int tag = Convert.ToInt32(pictureBoxVideo[i].Tag) - 1;
s.X = controlCloneVideo[i].Left;
s.Y = controlCloneVideo[i].Top;
s = controlCloneVideo[i].PointToScreen(new Point(0, 0));
if (((s.X <= p.X) && (p.X <= (s.X + controlCloneVideo[i].Width))) && ((s.Y <= p.Y) && (p.Y <= (s.Y + controlCloneVideo[i].Height))))
{
//将cloneVideo[i]与cloneVideo[a]位置交换
int x1 = controlCloneVideo[i].Left;
int y1 = controlCloneVideo[i].Top;
int height1 = controlCloneVideo[i].Height;
int width1 = controlCloneVideo[i].Width;
//controlCloneVideo[i].Height = 1;
//controlCloneVideo[i].Width = 1;
//controlCloneVideo[i].Left = -1;
//controlCloneVideo[i].Top = -1;
int x2 = controlCloneVideo[a].Left;
int y2 = controlCloneVideo[a].Top;
int height2 = controlCloneVideo[a].Height;
int width2 = controlCloneVideo[a].Width;
controlCloneVideo[i].Height = height2;
controlCloneVideo[i].Width = width2;
controlCloneVideo[i].Left = x2;
controlCloneVideo[i].Top = y2;
controlCloneVideo[a].Top = y1;
controlCloneVideo[a].Left = x1;
controlCloneVideo[a].Width = width1;
controlCloneVideo[a].Height = height1;
controlCloneVideo[a].Visible = true; //需要注意的地方(容易疏漏)
return;
}
}
}
private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
{
if (isDrag)
{
int n = Convert.ToInt32((sender as PictureBox).Tag) - 1;
for (int i = 0; i < 8; i++)
{
if (n == i)
{
snapShotPicture.Left += pictureBoxVideo[i].Left + Convert.ToInt16(e.X - snapShotPicture.Left);
snapShotPicture.Top += pictureBoxVideo[i].Top + Convert.ToInt16(e.Y - snapShotPicture.Top);
return;
}
}
}
}
private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
{
isDrag = true;
#region
// snapShotPicture.Left = e.X;
// snapShotPicture.Top = e.Y;
//截图
// Bitmap Pic = new Bitmap(138, 139);
// Graphics g = Graphics.FromImage(Pic);
//Bitmap Pic = new Bitmap(pictureBoxVideo[2].Width, pictureBoxVideo[2].Height);
//Graphics g = Graphics.FromImage(Pic);
//snapShotPicture.Left = pictureBoxVideo[2].Left;
//snapShotPicture.Top = pictureBoxVideo[2].Top;
//g.CopyFromScreen(pictureBoxVideo[2].PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(pictureBoxVideo[2].Width, pictureBoxVideo[2].Height));
//snapShotPicture.Image = Pic;
//snapShotPicture.Visible = true;
#endregion
int s = Convert.ToInt32((sender as PictureBox).Tag) - 1;
for (int i = 0; i < 8; i++)
{
if (s == i)
{
Bitmap Pic = new Bitmap(pictureBoxVideo[i].Width, pictureBoxVideo[i].Height);
Graphics g = Graphics.FromImage(Pic);
snapShotPicture.Left = pictureBoxVideo[s].Left;
snapShotPicture.Top = pictureBoxVideo[s].Top;
g.CopyFromScreen(pictureBoxVideo[s].PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(pictureBoxVideo[s].Width, pictureBoxVideo[s].Height));
snapShotPicture.Image = Pic;
snapShotPicture.Visible = true;
return;
}
}
}
}