我是编程新手。我在Visual Studio2019 WindowsForm中为一年级的孩子制作了一个应用程序,其中一个级别是将图片从PictureBox拖动到一个空的PictureBox中,该PictureBox有一个简单定义的标签。
所以如果我有两个PictureBox,假设有一只狗和一只鸡,还有两个空PictureBox (一个带有“骨骼”标签,另一个带有“谷物”标签)。我想将狗的图片拖到PictureBox中,标签为“骨骼”,鸡在标签为“谷粒”的图片中,如果正确,我会显示文本“伟大的工作”,如果不正确,我会显示文本“再试一次”。
我可以拖动图片,但我找不到一种方法来检查是否正确。有人能帮我吗?
到目前为止,我的代码如下:
private void NivelulDoi_Load(object sender, EventArgs e)
{
customPictureBox1.AllowDrop = true;
customPictureBox2.AllowDrop = true;
customPictureBox3.AllowDrop = true;
}
private void customPictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customPictureBox1.DoDragDrop(customPictureBox1.Image, DragDropEffects.Copy);
}
}
private void customPictureBox1_DragDrop(object sender, DragEventArgs e)
{
var data = e.Data.GetData(DataFormats.FileDrop);
if (data != null)
{
var fileNames = data as string[];
if (fileNames.Length > 0)
{
customPictureBox1.Image = Image.FromFile(fileNames[0]);
}
}
}
private void customPictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void customPictureBox2_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox2_DragDrop(object sender, DragEventArgs e)
{
customPictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
// figure out how to check if correct
/*if (customPictureBox2.Image == (Bitmap)e.Data.GetData(DataFormats.Bitmap, true))
{
label1.Text = "ai reusit";
}
else
{
label1.Text = "mai incearca";
}*/
}
private void customPictureBox3_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox3_DragDrop(object sender, DragEventArgs e)
{
customPictureBox3.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private void customPictureBox4_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox4_DragDrop(object sender, DragEventArgs e)
{
customPictureBox4.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private static void Dragging(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}谢谢。
发布于 2021-04-26 11:17:12
一种简单的方法是定义一个变量来保存"picturebox drag“的名称,并定义一个字典来保存"drag-drop”对。
这是演示。
string pictureBoxFrom = "";
Dictionary<string, string> picDict = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
pictureBox2.AllowDrop = true;
pictureBox4.AllowDrop = true;
pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox3.MouseDown += pictureBox_MouseDown;
pictureBox2.DragEnter += pictureBox_DragEnter;
pictureBox2.DragDrop += pictureBox_DragDrop;
pictureBox4.DragEnter += pictureBox_DragEnter;
pictureBox4.DragDrop += pictureBox_DragDrop;
picDict.Add("pictureBox1", "pictureBox2"); // drag 1 to 2
picDict.Add("pictureBox3", "pictureBox4"); // drag 3 to 4
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
pictureBoxFrom = (sender as PictureBox).Name;
var img = (sender as PictureBox).Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
{
//(sender as PictureBox).Image = null;
}
}
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
if (picDict.ContainsKey(pictureBoxFrom) && picDict[pictureBoxFrom].Equals((sender as PictureBox).Name))
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
(sender as PictureBox).Image = bmp;
MessageBox.Show("great job");
}
else
{
MessageBox.Show("try again");
}
}https://stackoverflow.com/questions/67240625
复制相似问题