您的位置:首页 > 编程语言 > C#

在C#中实现标签(Label)随鼠标移动而移动

2010-03-17 09:46 555 查看
文档及代码下载:http://www.dingos.cn/index.php?topic=1420.0

1. 定义一个Point对象mouse_offset,用于保存鼠标的位置
2. 对标签(Label)添加MouseDown事件,记录当前鼠标所在的位置为实例化mouse_offset对象

view plaincopy to clipboardprint?

private void lblMove_MouseDown(object sender, MouseEventArgs e) {

mouse_offset = new Point(-e.X, -e.Y);

}

3. 对标签(Label)添加MouseMove事件,实现标签(Label)随鼠标的移动而移动

view plaincopy to clipboardprint?

private void lblMove_MouseMove(object sender, MouseEventArgs e) {

if(e.Button == MouseButtons.Left) {

Point mousePos = Control.MousePosition;

mousePos.Offset(mouse_offset.X, mouse_offset.Y);

((Control)sender).Location =

((Control)sender).Parent.PointToClient(mousePos);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: