Label.MouseDown : Label « System.Windows.Forms « C# / C Sharp by API






Label.MouseDown

  




using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
  internal System.Windows.Forms.Label lblDragger;
  public Form1()
  {
    this.lblDragger = new System.Windows.Forms.Label();
    this.SuspendLayout();

    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);

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

}

   
    
  








Related examples in the same category

1.Label.Background
2.Label.Click
3.Label.DataBindings
4.Label.DoDragDrop
5.Label.Image
6.Label.ImageAlign
7.Label.ImageIndex
8.Label.ImageList
9.Label.Localtion
10.Label.Margin
11.Label.MouseHover
12.Label.MouseMove
13.Label.MouseUp
14.Label.Size
15.Label.Text