简单的说,就是处理这个控件的MouseDown,MouseMove,MouseUp事件,在控件的状态被置为选中后,在MouseMove事件中,改变这个控件的Location,在MouseUp事件中,停止移动,取消移动状态。
解决方案 »
- C#怎么在一个dll中引用另外一个dll?能这样做吗?哪位大虾救救我啊!!!
- 关于抓取数据的问题
- 紧急 求就 删除我在 dataGridView1 选中的一行 数据
- 调用存储过程时出错: 必须说明标识符
- 如何去掉转义\
- 已知一个DateTime变量,如何获得这个日期是本年的第几星期(1~53)?
- C#如何把一个string数组中不同的数据给另一个string数组
- C#写的WINDOWS程序总感觉有点慢
- C# format simple question
- 如何在WinXP RC2 2526上安装VS.Net Beta 2
- 为什么提示找不到System.Data.ADO?
- 高分请指教:如何用dataGrid实现编辑datatable中的单行记录(行式编辑窗口)?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace drag
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private System.ComponentModel.Container components = null;
ArrayList ControlsList,PointsList; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xLoc,yLoc;
Point newPoint;
Control b = new Control();
b = (Control)sender;
if(e.X >= 0 && e.X <= b.Size.Width)
xLoc = b.Location.X;
else
xLoc = e.X + b.Location.X; if(e.Y >= 0 && e.Y <= b.Size.Height)
yLoc = b.Location.Y;
else
yLoc = e.Y + b.Location.Y;
newPoint = new System.Drawing.Point(xLoc,yLoc);
for(int i = 0;i < 4; i++)
{
Point tempPoint = (Point)PointsList[i];
Control tempControl = (Control)ControlsList[i];
if( newPoint.Y >= tempPoint.Y && newPoint.Y <= tempPoint.Y + tempControl.Size.Height)
{
ControlsList.Remove(b);
ControlsList.Insert(i,b);
break;
}
}
Point firstPoint,lastPoint;
firstPoint = (Point)PointsList[0];
lastPoint = (Point)PointsList[3];
if(newPoint.Y < firstPoint.Y)
{
ControlsList.Remove(b);
ControlsList.Insert(0,b);
}
else if(newPoint.Y > lastPoint.Y)
{
ControlsList.Remove(b);
ControlsList.Insert(3,b);
} for(int i = 0;i <4 ; i++)
{
Control tempControl = (Control)ControlsList[i];
Point tempPoint = (Point)PointsList[i];
tempControl.Location = tempPoint;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
ControlsList = new ArrayList(4);
PointsList = new ArrayList(4);
for(int i = 1; i< 5; i++)
{
Button bt = new Button();
bt.Location = new System.Drawing.Point(24, 40+23*(i-1));
Point p = bt .Location;
PointsList.Add(p);
bt.Name = "button" + i.ToString();
bt.TabIndex = 0;
bt.Text = "button" + i.ToString();
bt.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button_MouseUp);
this.Controls.Add(bt);
ControlsList.Add(bt);
} } private void button1_Click(object sender, System.EventArgs e)
{
} }
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace myGDI
{
/// <summary>
/// CustomControl1 的摘要说明。
/// </summary>
public class CustomControl1 : System.Windows.Forms.Control
{
private bool mousein=false;
private int x;
private int y;
private bool isSelect=false; public CustomControl1()
{
this.Size=new Size(10,10);
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
this.SetStyle(ControlStyles.Selectable,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.UserMouse,true);
this.BackColor=Color.FromArgb(0,Color.Blue);
}
public int X
{
get
{
return this.Left+this.Width/2;
}
set
{
this.x=value;
this.Left=value-this.Width/2;
}
}
public int Y
{
get
{
return this.Top +this.Height /2;
}
set
{
this.y=value;
this.Top =value-this.Height /2;
}
}
public bool IsSelect
{
get
{
return this.isSelect;
}
set
{
this.isSelect=value;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此添加自定义绘画代码
Graphics g=pe.Graphics; if (this.isSelect)
g.FillRectangle(new SolidBrush(Color.FromArgb(200,Color.Blue)),this.ClientRectangle);
else
{
// g.FillRectangle(new SolidBrush(Color.FromArgb(0,Color.Blue)),this.ClientRectangle);
if (this.mousein)
{
g.DrawRectangle(Pens.Blue,0,0,this.Width-1,this.Height-1);
}
else
{
g.DrawRectangle(Pens.Blue,this.Width/4,this.Height/4,this.Width/2,this.Height/2);
}
}
// 调用基类 OnPaint
base.OnPaint(pe);
}
protected override void OnMouseEnter(EventArgs e)
{
if (!this.isSelect)
{
base.OnMouseEnter(e);
this.mousein=true;
this.Refresh();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (!this.isSelect)
{
base.OnMouseDown(e);
this.isSelect=true;
this.Refresh();
}
} protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
this.isSelect=false;
this.mousein=false;
this.Refresh();
}
protected override void OnMouseLeave(EventArgs e)
{
if (!this.isSelect)
{
base.OnMouseLeave(e);
this.mousein =false;
this.Refresh();
}
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{ int xLoc,yLoc;
if(e.X >= 0 && e.X <= this.Size.Width)
xLoc = this.Location.X;
else
xLoc = e.X + this.Location.X; if(e.Y >= 0 && e.Y <= this.Size.Height)
yLoc = this.Location.Y;
else
yLoc = e.Y + this.Location.Y; this.Location = new Point(xLoc,yLoc);
base.OnMouseUp (e);
}
就可以了
protected override void OnMouseMove(MouseEventArgs e)
{ int xLoc,yLoc;
if(e.X >= 0 && e.X <= this.Size.Width)
xLoc = this.Location.X;
else
xLoc = e.X + this.Location.X; if(e.Y >= 0 && e.Y <= this.Size.Height)
yLoc = this.Location.Y;
else
yLoc = e.Y + this.Location.Y; this.Location = new Point(xLoc,yLoc);
base.OnMouseMove (e);
}