控件的拖动和缩放

using System;
using System.Drawing;
using System.Windows.Forms;


public class Form1 : System.Windows.Forms.Form
  {
   Form1()
   {
    InitializeComponent();
   }
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }
   enum EnumMousePointPosition
   {
    MouseSizeNone = 0,
    MouseSizeRight = 1,
    MouseSizeLeft = 2,
    MouseSizeBottom = 3,
    MouseSizeTop = 4,
    MouseSizeTopLeft = 5,
    MouseSizeTopRight = 6,
    MouseSizeBottomLeft = 7,
    MouseSizeBottomRight = 8,
    MouseDrag = 9
   }
   EnumMousePointPosition m_MousePointPosition;
   Point p;
 private System.Windows.Forms.Button button1;
         Point p1;

   private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
    p = new Point(e.X, e.Y);
    p1 = new Point(e.X, e.Y);
   }

   private void MyMouseLeave(object sender, System.EventArgs e)
   {
    m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
    this.Cursor = Cursors.Arrow;
   }

   private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
   {
    if (e.Button == MouseButtons.Left)
    {
     if (m_MousePointPosition == EnumMousePointPosition.MouseDrag)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top + e.Y - p.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottom)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width, ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomRight)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width + e.X - p1.X, ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeRight)
     {
      ((Button)sender).Size = new Size(((Button)sender).Width + e.X - p1.X, ((Button)sender).Height);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTop)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width, ((Button)sender).Height - (e.Y - p.Y));
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top);
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top);
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height + e.Y - p1.Y);
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopRight)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width + (e.X - p1.X), ((Button)sender).Height - (e.Y - p.Y));
      p1 = new Point(e.X, e.Y);
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopLeft)
     {
      ((Button)sender).Location = new Point(((Button)sender).Left + e.X - p.X, ((Button)sender).Top + (e.Y - p.Y));
      ((Button)sender).Size = new Size(((Button)sender).Width - (e.X - p.X), ((Button)sender).Height - (e.Y - p.Y));
     }
    }
    else
    {
     m_MousePointPosition = MousePointPosition(((Button)sender).Size, e);
     if (m_MousePointPosition == EnumMousePointPosition.MouseSizeNone)
     {
      this.Cursor = Cursors.Arrow;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseDrag)
     {
      this.Cursor = Cursors.SizeAll;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottom)
     {
      this.Cursor = Cursors.SizeNS;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTop)
     {
      this.Cursor = Cursors.SizeNS;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeLeft)
     {
      this.Cursor = Cursors.SizeWE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeRight)
     {
      this.Cursor = Cursors.SizeWE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomLeft)
     {
      this.Cursor = Cursors.SizeNESW;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeBottomRight)
     {
      this.Cursor = Cursors.SizeNWSE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopLeft)
     {
      this.Cursor = Cursors.SizeNWSE;
     }
     else if (m_MousePointPosition == EnumMousePointPosition.MouseSizeTopRight)
     {
      this.Cursor = Cursors.SizeNESW;
     }
    }
   }

   private EnumMousePointPosition MousePointPosition(Size Size, System.Windows.Forms.MouseEventArgs e)
   {
    const int Band = 10;
    if (e.X >= -1 * Band & e.X <= Size.Width & e.Y >= -1 * Band & e.Y <= Size.Height)
    {
     if (e.X < Band)
     {
      if (e.Y < Band)
      {
       return EnumMousePointPosition.MouseSizeTopLeft;
      }
      else if (e.Y > -1 * Band + Size.Height)
      {
       return EnumMousePointPosition.MouseSizeBottomLeft;
      }
      else
      {
       return EnumMousePointPosition.MouseSizeLeft;
      }
     }
     else if (e.X > -1 * Band + Size.Width)
     {
      if (e.Y < Band)
      {
       return EnumMousePointPosition.MouseSizeTopRight;
      }
      else if (e.Y > -1 * Band + Size.Height)
      {
       return EnumMousePointPosition.MouseSizeBottomRight;
      }
      else
      {
       return EnumMousePointPosition.MouseSizeRight;
      }
     }
     else
     {
      if (e.Y < Band)
      {
       return EnumMousePointPosition.MouseSizeTop;
      }
      else if (e.Y > -1 * Band + Size.Height)
      {
       return EnumMousePointPosition.MouseSizeBottom;
      }
      else
      {
       return EnumMousePointPosition.MouseDrag;
      }
     }
    }
    else
    {
     return EnumMousePointPosition.MouseSizeNone;
    }
   }

 private void InitializeComponent()
 {
  this.button1 = new System.Windows.Forms.Button();
  this.SuspendLayout();
  //
  // button1
  //
  this.button1.Location = new System.Drawing.Point(0, 0);
  this.button1.Name = "button1";
  this.button1.TabIndex = 0;
  this.button1.Text = "button1";
  this.button1.Click += new System.EventHandler(this.button1_Click);
  //
  // Form1
  //
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(440, 301);
  this.Controls.Add(this.button1);
  this.Name = "Form1";
  this.ResumeLayout(false);

 }

   

 private void button1_Click(object sender, System.EventArgs e)
 {
  Button Button = new Button();
  Controls.Add(Button);
  Button.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
  Button.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
  Button.MouseLeave += new System.EventHandler(MyMouseLeave);
 }
  }
 

posted on 2005-11-03 09:43  井泉  阅读(350)  评论(0编辑  收藏  举报

导航