-1

我有一个带有 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";
    }
}
}
4

2 回答 2

0

没关系,我想通了:

If (sender == label1)

lblMsg.Text = "You Picked Red";

我让它变得更难然后它需要

于 2012-08-26T21:49:18.140 回答
0

您发布的内容将起作用,但您也可以向图像添加标签,这样您就不必依赖标签的名称来确定丢弃的图像。看看这样的事情是否适合你。

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();
        img1.Tag = 1;
        img2.Tag = 2;
        img3.Tag = 3;
        img4.Tag = 4;
        label1.Image = img1;
        label2.Image = img2;
        label3.Image = img3;
        label4.Image = img4;

    }

    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();
        var image = (Image)e.Data.GetData(typeof(Bitmap));
        var index = (int)image.Tag;
        g.DrawImage(image, new Point(0, 0));

        switch (index)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            default:
                break;
        }
        g.Dispose();
        image.Dispose();

    } 

}
于 2012-08-26T22:54:41.603 回答