Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c# - Drag-and-Drop an Image

I have a form with 6 labels 4 of them have png Images in them. I have it set up so the user can drag one of the 4 images into label5 and label6 would give them a message to tell them which of the 4 they picked I have the drag drop part working but cannot figure out what code I need to tell them which was picked.

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";
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Never mind I figured it out:

If (sender == label1)

lblMsg.Text = "You Picked Red";

I was making it harder then it needed to be


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...