110,547
社区成员
发帖
与我相关
我的任务
分享
//Form窗体上实现3个事件
private Point mousePos;
private bool beginmove = false;
private void duxFeePanelCtl1_OnMouseDown(object sender, MouseEventArgs e)
{
beginmove = true;//开始移动
mousePos = new Point(e.X, e.Y);
}
private void duxFeePanelCtl1_OnMouseMove(object sender, MouseEventArgs e)
{
if (beginmove)
{
int diffx, diffy;
diffx = mousePos.X - e.X;
diffy = mousePos.Y - e.Y;
this.duxFeePanelCtl1.Location = new Point(duxFeePanelCtl1.Location.X - diffx, duxFeePanelCtl1.Location.Y - diffy);
}
}
private void duxFeePanelCtl1_OnMouseUp(object sender, MouseEventArgs e)
{
beginmove = false;//停止移动
}
/*能够实现控件的拖动,我现在想把这个方法封装在自定义控件里面,他本身就能实现拖动,而不要再添加他的Form窗体上去写Mouse事件*/
/*或者写一个什么样的类去实现下*/
public partial class Moveable : UserControl
{
private Point m_MousePoint;
private Point m_LastPoint;
public Moveable()
{
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.m_LastPoint = this.Location;
this.m_MousePoint = this.PointToScreen(e.Location);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
Point t = this.PointToScreen(e.Location);
Point l = this.m_LastPoint;
l.Offset(t.X - this.m_MousePoint.X, t.Y - this.m_MousePoint.Y);
this.Location = l;
}
}
}