这篇文章主要为大家详细介绍了winform实现可拖动的自定义Label控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了winform可拖动的自定义Label控件,供大家参考,具体内容如下
效果预览:
public partial class LabelModule : LabelControl
(3)这个Label需要实现的MouseDown。
private void LabelModule_MouseDown(object sender, MouseEventArgs e)
{
IsMouseDown = true;
MousePrePosition = new Point(e.X, e.Y);
this.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
this.Cursor = Cursors.SizeAll;
}
(4)MouseUp,也就是鼠标弹起的方法。
private void LabelModule_MouseUp(object sender, MouseEventArgs e)
{
IsMouseDown = false;
this.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
this.Cursor = Cursors.Default;
}
(5)MouseMove,也就是鼠标移动时的方法。
private void LabelModule_MouseMove(object sender, MouseEventArgs e)
{
if (!IsMouseDown) return;
this.Top = this.Top + (e.Y - MousePrePosition.Y);
this.Left = this.Left + (e.X - MousePrePosition.X);
}
e.X,e.Y 指的是:鼠标的坐标因所引发的事件而异。例如,当处理 Control.MouseMove 事件时,鼠标的坐标值是相对于引发事件的控件的坐标。一些与拖放操作相关的事件具有相对于窗体原点或屏幕原点的关联的鼠标坐标值。
完整代码:LabelModule.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace IJPrinterSoftware
{
public partial class LabelModule : LabelControl
{
private bool IsMouseDown = false;
private Point MousePrePosition;
private void init()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(LabelModule_MouseDown);
this.MouseUp += new MouseEventHandler(LabelModule_MouseUp);
this.MouseMove+=new MouseEventHandler(LabelModule_MouseMove);
}
public LabelModule()
{
init();
}
private void LabelModule_MouseDown(object sender, MouseEventArgs e)
{
IsMouseDown = true;
MousePrePosition = new Point(e.X, e.Y);
this.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
this.Cursor = Cursors.SizeAll;
}
private void LabelModule_MouseUp(object sender, MouseEventArgs e)
{
IsMouseDown = false;
this.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
this.Cursor = Cursors.Default;
}
private void LabelModule_MouseMove(object sender, MouseEventArgs e)
{
if (!IsMouseDown) return;
this.Top = this.Top + (e.Y - MousePrePosition.Y);
this.Left = this.Left + (e.X - MousePrePosition.X);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:winform实现可拖动的自定义Label控件


猜你喜欢
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- ubuntu下C/C++获取剩余内存 2023-09-18
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言详解float类型在内存中的存储方式 2023-03-27
- Qt计时器使用方法详解 2023-05-30
- C语言qsort()函数的使用方法详解 2023-04-26
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- Easyx实现扫雷游戏 2023-02-06
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11