读书人

一个拖动UserControl的有关问题

发布时间: 2013-07-01 12:33:04 作者: rapoo

一个拖动UserControl的问题
新建一个UserControl,在其中增加两个lable,设置其中一个Dock属性为 Fill
要求鼠标点在那个填充的lable上也要能使整个UserControl被拖动.
拖动UserControl
[解决办法]
如果是 winform


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

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private Point mPoint = new Point();

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mPoint.X = e.X;
mPoint.Y = e.Y;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosittion = MousePosition;
myPosittion.Offset(-mPoint.X, -mPoint.Y);
Location = myPosittion;


}
}
}
}



楼主只需要把 Form1_MouseMove 改成 lable_MouseMove即可
[解决办法]
直接把需要点击的控件的mousedown和mousemove事件都注册到2楼提供的方法里就可以了

读书人网 >C#

热点推荐