拖放图像 [英] Drag-and-Drop an Image

查看:31
本文介绍了拖放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 6 个标签的表单,其中 4 个有 png 图像.我已经设置好了,所以用户可以将 4 张图像中的一张拖到 label5 中,label6 会给他们一条消息,告诉他们他们选择了 4 张图片中的哪一张我有拖放部分工作,但不知道我需要告诉他们哪些代码被选中.

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";
    }
}
}

推荐答案

没关系我想通了:

If (sender == label1)

lblMsg.Text = "You Picked Red";

我让它变得更难了

这篇关于拖放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆