Csharp/C Sharp/GUI Windows Form/Drag Drop — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:33, 26 мая 2010

Drag and drop image to another window

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Palette : Form
{
  private System.Windows.Forms.Label lblPictureThree;
  private System.Windows.Forms.Label lblPictureTwo;
  private System.Windows.Forms.Label lblPictureOne;
  public Palette() {
        InitializeComponent();
  }
  private void lbl_MouseDown(object sender, MouseEventArgs e)
  {
    Label lbl = (Label)sender;
    lbl.DoDragDrop(lbl.Image, DragDropEffects.Copy);
  }
  private void InitializeComponent()
  {
    this.lblPictureThree = new System.Windows.Forms.Label();
    this.lblPictureTwo = new System.Windows.Forms.Label();
    this.lblPictureOne = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblPictureThree
    // 
    this.lblPictureThree.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureThree.Image = new Bitmap("winter.jpg");
    this.lblPictureThree.Location = new System.Drawing.Point(12, 113);
    this.lblPictureThree.Name = "lblPictureThree";
    this.lblPictureThree.Size = new System.Drawing.Size(56, 48);
    this.lblPictureThree.TabIndex = 6;
    this.lblPictureThree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureTwo
    // 
    this.lblPictureTwo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureTwo.Image = new Bitmap("winter.jpg");
    this.lblPictureTwo.Location = new System.Drawing.Point(12, 61);
    this.lblPictureTwo.Name = "lblPictureTwo";
    this.lblPictureTwo.Size = new System.Drawing.Size(56, 48);
    this.lblPictureTwo.TabIndex = 5;
    this.lblPictureTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureOne
    // 
    this.lblPictureOne.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureOne.Image = new Bitmap("winter.jpg");
    this.lblPictureOne.Location = new System.Drawing.Point(12, 9);
    this.lblPictureOne.Name = "lblPictureOne";
    this.lblPictureOne.Size = new System.Drawing.Size(56, 48);
    this.lblPictureOne.TabIndex = 4;
    this.lblPictureOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // Palette
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(83, 173);
    this.Controls.Add(this.lblPictureTwo);
    this.Controls.Add(this.lblPictureOne);
    this.Controls.Add(this.lblPictureThree);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
    this.Name = "Palette";
    this.ShowInTaskbar = false;
    this.Text = "Palette";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main()
  {
    Application.Run(new DrawingArea());
  }
}
  public class DrawingArea : Form
  {
    private System.Windows.Forms.PictureBox picDrawingArea;
    public DrawingArea()
    {
      InitializeComponent();
    }
    private void DrawingArea_Load(object sender, EventArgs e)
    {
      Palette frmTool = new Palette();
      this.AddOwnedForm(frmTool);
      frmTool.Show();
      picDrawingArea.AllowDrop = true;
    }
    private void picDrawingArea_DragEnter(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.Bitmap))
      {
        e.Effect = DragDropEffects.Copy;
      }
      else
      {
        e.Effect = DragDropEffects.None;
      }
    }
    private void picDrawingArea_DragDrop(object sender, DragEventArgs e)
    {
      Graphics g = picDrawingArea.CreateGraphics();
      g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap),
        new Point(e.X - this.Left, e.Y - this.Top));
    }
     private void InitializeComponent()
    {
      this.picDrawingArea = new System.Windows.Forms.PictureBox();
      ((System.ruponentModel.ISupportInitialize)(this.picDrawingArea)).BeginInit();
      this.SuspendLayout();
      // 
      // picDrawingArea
      // 
      this.picDrawingArea.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
      this.picDrawingArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.picDrawingArea.Location = new System.Drawing.Point(1, 2);
      this.picDrawingArea.Name = "picDrawingArea";
      this.picDrawingArea.Size = new System.Drawing.Size(377, 270);
      this.picDrawingArea.TabIndex = 2;
      this.picDrawingArea.TabStop = false;
      this.picDrawingArea.DragDrop += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragDrop);
      this.picDrawingArea.DragEnter += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragEnter);
      // 
      // DrawingArea
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(379, 274);
      this.Controls.Add(this.picDrawingArea);
      this.Name = "DrawingArea";
      this.Text = "Drawing Area";
      this.Load += new System.EventHandler(this.DrawingArea_Load);
      ((System.ruponentModel.ISupportInitialize)(this.picDrawingArea)).EndInit();
      this.ResumeLayout(false);
    }
  }


Drag and drop inside a container

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
  internal System.Windows.Forms.Label lblDragger;
  public Form1()
  {
    InitializeComponent();
  }
  private void InitializeComponent()
  {
    this.lblDragger = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblDragger
    // 
    this.lblDragger.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblDragger.Image = new Bitmap("winter.jpg");
    this.lblDragger.Location = new System.Drawing.Point(110, 105);
    this.lblDragger.Name = "lblDragger";
    this.lblDragger.Size = new System.Drawing.Size(72, 56);
    this.lblDragger.TabIndex = 2;
    this.lblDragger.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseUp);
    this.lblDragger.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseMove);
    this.lblDragger.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseDown);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.lblDragger});
    this.Name = "Form1";
    this.Text = "Fake Drag And Drop";
    this.ResumeLayout(false);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new Form1());
  }

  private bool isDragging = false;

  private int clickOffsetX, clickOffsetY;

  private void lblDragger_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
  {
    isDragging = true;
    clickOffsetX = e.X;
    clickOffsetY = e.Y;
  }
  private void lblDragger_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
  {
    isDragging = false;
  }
  private void lblDragger_MouseMove(System.Object sender,
    System.Windows.Forms.MouseEventArgs e)
  {
    if (isDragging == true)
    {
      lblDragger.Left = e.X + lblDragger.Left - clickOffsetX;
      lblDragger.Top = e.Y + lblDragger.Top - clickOffsetY;
    }
  }
}


Drag and drop the PictureBox

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Form1 : System.Windows.Forms.Form
  {
    private bool  isDragging = false;
    private int   currentX, currentY;
    Rectangle dropRect = new Rectangle(180, 180, 60, 60);
    private PictureBox myPictureBox; 
    public Form1()
    {
      InitializeComponent();
      CenterToScreen();
      myPictureBox = new PictureBox();
      myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
      myPictureBox.Location = new System.Drawing.Point(64, 32);
      myPictureBox.Size = new System.Drawing.Size(50, 50);
      myPictureBox.Image = new Bitmap("winter.jpg");
      myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown);
      myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp);
      myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove);
      myPictureBox.Cursor = Cursors.Hand;
      Controls.Add(myPictureBox);
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Dragging Images";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void myPictureBox_MouseDown(object sender, MouseEventArgs e) 
    {
      isDragging = true;
      currentX = e.X;
      currentY = e.Y;
    }
    private void myPictureBox_MouseMove(object sender, MouseEventArgs e) {
      if (isDragging) {
        myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
        myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
      }
    }
    private void myPictureBox_MouseUp(object sender, MouseEventArgs e) 
    {
      isDragging = false;
      Console.WriteLine(dropRect.Contains(myPictureBox.Bounds));
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.AntiqueWhite, dropRect);
      g.DrawString("Drag and drop the image here.", new Font("Times New Roman", 8), Brushes.Red, dropRect);
    }
  }


Fake Drag And Drop

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FakeDragAndDrop
{
  /// <summary>
  /// Summary description for FakeDragAndDrop.
  /// </summary>
  public class FakeDragAndDrop : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.Label lblDragger;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ruponentModel.Container components = null;
    public FakeDragAndDrop()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FakeDragAndDrop));
      this.lblDragger = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // lblDragger
      // 
      this.lblDragger.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.lblDragger.Image = ((System.Drawing.Bitmap)(resources.GetObject("lblDragger.Image")));
      this.lblDragger.Location = new System.Drawing.Point(110, 105);
      this.lblDragger.Name = "lblDragger";
      this.lblDragger.Size = new System.Drawing.Size(72, 56);
      this.lblDragger.TabIndex = 2;
      this.lblDragger.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseUp);
      this.lblDragger.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseMove);
      this.lblDragger.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseDown);
      // 
      // FakeDragAndDrop
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.lblDragger});
      this.Name = "FakeDragAndDrop";
      this.Text = "Fake Drag And Drop";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FakeDragAndDrop());
    }
    // Keep track of when fake "drag and drop" mode is enabled.
    private bool isDragging = false;
    // Store the location where the user clicked the control.
    private int clickOffsetX, clickOffsetY;
    // Start dragging.
    private void lblDragger_MouseDown(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      isDragging = true;
      clickOffsetX = e.X;
      clickOffsetY = e.Y;
    }
    // End dragging.
    private void lblDragger_MouseUp(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      isDragging = false;
    }
    // Move the control (during dragging).
    private void lblDragger_MouseMove(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      if (isDragging == true)
      {
        // The control coordinates are converted into form coordinates
        // by adding the label position offset.
        // The offset where the user clicked in the control is also
        // accounted for. Otherwise, it looks like the top-left corner
        // of the label is attached to the mouse.
        lblDragger.Left = e.X + lblDragger.Left - clickOffsetX;
        lblDragger.Top = e.Y + lblDragger.Top - clickOffsetY;
      }
    }
  }
}

<A href="http://www.nfex.ru/Code/CSharpDownload/FakeDragAndDrop.zip">FakeDragAndDrop.zip( 23 k)</a>


Image Drop

 
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
class ImageDrop : Form {
    bool bIsTarget;
    Image image;
    public static void Main() {
        Application.Run(new ImageDrop());
    }
    public ImageDrop() {
        AllowDrop = true;
    }
    protected override void OnDragOver(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop) || dea.Data.GetDataPresent(typeof(Metafile)) || dea.Data.GetDataPresent(typeof(Bitmap))) {
            if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
                dea.Effect = DragDropEffects.Move;
            if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) && ((dea.KeyState & 0x08) != 0))    // Ctrl key
                dea.Effect = DragDropEffects.Copy;
        }
    }
    protected override void OnDragDrop(DragEventArgs dea) {
        if (dea.Data.GetDataPresent(DataFormats.FileDrop)) {
            string[] astr = (string[])dea.Data.GetData(DataFormats.FileDrop);
            image = Image.FromFile(astr[0]);
            Invalidate();
        } else {
            if (dea.Data.GetDataPresent(typeof(Metafile)))
                image = (Image)dea.Data.GetData(typeof(Metafile));
            else if (dea.Data.GetDataPresent(typeof(Bitmap)))
                image = (Image)dea.Data.GetData(typeof(Bitmap));
            bIsTarget = true;
            Invalidate();
        }
    }
    protected override void OnMouseDown(MouseEventArgs mea) {
        if (image != null) {
            bIsTarget = false;
            DragDropEffects dde = DoDragDrop(image,DragDropEffects.Copy | DragDropEffects.Move);
            if (dde == DragDropEffects.Move && !bIsTarget)
                image = null;
        }
    }
}