效果预览:




实现步骤如下:

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

        }

    }

}

版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

winform 可拖动的自定义Label控件的更多相关文章

  1. 自定义Label控件

    最近开发过程中有一个需求就是修改label控件的模板,使其能够在鼠标移近的时候变成TextBox,从而方便输入,然后进行相应的修改,最终达到动态修改Label的目的,这里贴出相应的代码,并做简要的分析 ...

  2. winform学习(7)Label控件、Button控件、TextBox控件

    Label控件是System.Windows.Forms.Label 类提供的控件. 作用:主要用来提供其他控件的描述文字,例如:登录窗体上的用户名.密码(输入框前面的字) Button控件是Syst ...

  3. C# winform自定义Label控件使其能设置行距

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. C#Winform VScrollBar+Pannel自定义列表控件(原)

    该控件的主要实现思路是用的objective-c中的自定义控件思路,主视图中放子视图 效果图 (窗体调用代码) public partial class Form1 : RibbonForm { Li ...

  5. Winform自定义键盘控件开发及使用

    最近有学员提出项目中要使用键盘控件,系统自带的osk.exe不好用,于是就有了下面的内容: 首先是进行自定义键盘控件的开发,其实核心大家都知道,就是利用SendKeys.Send发送相应 的字符,但是 ...

  6. 在Winform界面使用自定义用户控件及TabelPanel和StackPanel布局控件

    在很多时候,我们做一些非常规化的界面的时候,往往需要创建一些用户控件,在其中绘制好一些基础的界面块,作为后续重复使用的一个单元,用户控件同时也可以封装处理一些简单的逻辑.在开发Winform各种类型项 ...

  7. Label控件如何根据字符串自定义大小

    一.. this.label_Msg.AutoSize = false;  //设置label空件不能自动大小 二.. 用代码控制label控件的大小 1.根据字符串.label的宽度 计算字符串的面 ...

  8. WinForm使用Label控件模拟分割线(竖向)

    用Label控件进行模拟 宽度设为1:this.lblPagerSpliter1.Size = new System.Drawing.Size(1, 21); 去掉边框:this.lblPagerSp ...

  9. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

随机推荐

  1. 电子商务中:B2C、B2B、C2B、C2C、O2O、P2P

     c2c实际是电子商务的专业用语,是个人与个人之间的电子商务.比如一个消费者有一台电脑,通过网络进行交易,把它出售给另外一个消费者,此种交易类型就称为C2C电子商务.淘宝是属于C2C模式的.   c2 ...

  2. Scalaz(20)-Monad: Validation-Applicative版本的Either

    scalaz还提供了个type class叫Validation.乍看起来跟\/没什么分别.实际上这个Validation是在\/的基础上增加了Applicative功能,就是实现了ap函数.通过Ap ...

  3. TestNG官方文档中文版(1)-介绍

    TestNG的官方文档请见: http://testng.org/doc/documentation-main.html 1. 介绍    TestNG是一个设计用来简化广泛的测试需求的测试框架,从单 ...

  4. svg-高斯模糊+swiper伦播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Javascript 语言精粹 代码片段合集

    Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...

  6. Horseman - 让你更轻松的使用 PhantomJS

    Horseman 是一个 Node.js 模块,让你可以更轻松的使用 PhantomJS 进行功能测试,页面自动机,网络监控,屏幕捕获等.它提供了直接,链式的 API,易于理解的控制流,避免回调陷阱. ...

  7. ae开发基础功能

    放大.缩小.pan.框选要素(新建命令,建立命令HOOK,赋给当前地图控件): ICommand cmd = new ControlsSelectFeaturesTool(); cmd.OnCreat ...

  8. 读取SHP的投影信息

    from osgeo import ogr shp_driver = ogr.GetDriverByName('ESRI Shapefile') shp_dataset = shp_driver.Op ...

  9. atitit.数据验证--db数据库数据验证约束

    atitit.数据验证--db数据库数据验证约束 1. 为了加强账户数据金额的安全性,需要增加验证字段..1 2. 创建帐户1 3. 更改账户2 4. ---code3 5. --fini4 1. 为 ...

  10. Thoughts on an Article from Science 'A network framework of cultural history'

    Apparently, this is an excellent interdisciplinary study. This paper drawn on a simple but large dat ...