winform 可拖动的自定义Label控件

效果预览:

 


 

实现步骤如下:

 

(1)首先在项目上右击选择:添加->新建项,添加自定义控件

 

 


 

(2)自定义的一个Label让它继承LabelControl控件,LabelControl控件是DevExpress控件库里面的一种,和Label控件差不多,想了解更多关于DevExpress控件,推荐到DevExpress控件论坛学习:http://www.dxper.net/

 

    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);
        }
    }
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒饭君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值