我有一个带有 6 个标签的表单,其中 4 个标签中有 png 图像。我已经设置好了,因此用户可以将 4 个图像中的一个拖到 label5 中,而 label6 会给他们一条消息,告诉他们他们选择的 4 个图像中的哪一个我有拖放部分工作,但无法弄清楚我需要告诉什么代码他们被选中了。
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;
using System.IO;
namespace Drag_Drop_Tester2
{
public partial class Form1 : Form
{
Image img1 = Image.FromFile("Peg_Red.png");
Image img2 = Image.FromFile("Peg_Blue.png");
Image img3 = Image.FromFile("Peg_Green.png");
Image img4 = Image.FromFile("Peg_Orange.png");
public Form1()
{
InitializeComponent();
}
private void DD_MouseDown(object sender, MouseEventArgs e)
{
Label lblPic = (Label)sender;
lblPic.DoDragDrop(lblPic.Image, DragDropEffects.Copy);
}
private void DD_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Bitmap)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void DD_DragDrop(object sender, DragEventArgs e)
{
Label lblPic = (Label)sender;
Graphics g = lblPic.CreateGraphics();
g.DrawImage((Image)e.Data.GetData(typeof(Bitmap)), new Point(0, 0));
if ("code that goes here")
lblMsg.Text = "You picked red";
else
lblMsg.Text = "I can't decide what you picked";
}
}
}