1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace MouseMoveBtn
{
public partial class MovableBtn : Button
{
private bool allowMove;
private Timer timer = new Timer();
private Point refPoint = new Point();
[DefaultValue(true)]
public bool AllowMove
{
get { return allowMove; }
set { allowMove = value; }
}
private bool canMove = false;
public MovableBtn()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 2000;
}
protected override void OnMouseMove(MouseEventArgs mevent)
{
base.OnMouseMove(mevent);
if (mevent.Button == MouseButtons.Left)
{
if (canMove)
{
int deltaX = MousePosition.X - refPoint.X;
int deltaY = MousePosition.Y - refPoint.Y;
Location = new Point(Location.X + deltaX, Location.Y + deltaY);
refPoint = MousePosition;
}
else
{
timer.Stop();
timer.Start();
}
}
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
canMove = true;
Cursor.Current = Cursors.Cross;
refPoint = MousePosition;
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
timer.Start();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
canMove = false;
Cursor.Current = Cursors.Hand;
timer.Stop();
}
}
} |