using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Jigsaw : Form
{
public Jigsaw()
{
InitializeComponent();
PBoxJigsaw1.MouseDown += PBoxJigsaw1_MouseDown;
pnlJigsaw1.AllowDrop = true;
pnlJigsaw1.DragEnter += pnlJigsaw1_DragEnter;
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
JigsawLabel.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
}
private void HomeButton_Click(object sender, EventArgs e)
{
MainScreen main = new MainScreen();
main.Show();
this.Hide();
}
private int xPos;
private int yPos;
private void PBoxJigsaw1_MouseDown(object sender, MouseEventArgs e)
{
var img = PBoxJigsaw1.Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
{
PBoxJigsaw1.Image = null;
}
}
private void PBoxJigsaw1_MouseMove(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
((PictureBox)sender).Top += (e.Y - yPos);
((PictureBox)sender).Left += (e.X - xPos);
this.Refresh();
}
}
}
private void PBoxJigsaw1_MouseUp(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox answer = (PictureBox)sender;
if (answer.Location.X < pnlJigsaw1.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw1.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw1.Location.X + pnlJigsaw1.Width)
{
answer.Location = pnlJigsaw1.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw2.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw2.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw2.Location.X + pnlJigsaw2.Width)
{
answer.Location = pnlJigsaw2.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw3.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw3.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw3.Location.X + pnlJigsaw3.Width)
{
answer.Location = pnlJigsaw3.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw4.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw4.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw4.Location.X + pnlJigsaw4.Width)
{
answer.Location = pnlJigsaw4.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw5.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw5.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw5.Location.X + pnlJigsaw5.Width)
{
answer.Location = pnlJigsaw5.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw6.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw6.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw6.Location.X + pnlJigsaw6.Width)
{
answer.Location = pnlJigsaw6.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw7.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw7.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw7.Location.X + pnlJigsaw7.Width)
{
answer.Location = pnlJigsaw7.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw8.Location.X)
{
if (answer.Location.X + answer.Width > pnlJigsaw8.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw8.Location.X + pnlJigsaw8.Width)
{
answer.Location = pnlJigsaw8.Location;
}
}
}
}
}
}
private void pnlJigsaw1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
private void pnlJigsaw2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw3_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw4_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw5_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw6_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw7_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pnlJigsaw8_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
}
}
我正在尝试使用拖放操作在应用程序的窗口上制作拼图游戏。我有8个pictureBox和8个面板供picBox使用。上面附带的代码允许我将picBox拖动到前4个面板中,然后它们会卡入到位,但代码无法让piBox卡入底部的4个面板。如果我把面板5的代码,例如,面板1的代码上面,picBox thens卡入面板5而不是面板1.当mouseUp发生时,我是否需要确定pictureBox的位置(可能是y co-纵坐标)?或者我该如何解决这个问题?
提前致谢