效果预览:




实现步骤如下:

(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. Java--如何使用sun.misc.Unsafe完成compareAndSwapObject原子操作

    package com; import sun.misc.Unsafe; import java.lang.reflect.Field; /** * Created by yangyu on 16/1 ...

  2. [转载]协程-cooperative multitasking

    [转载]协程三讲 http://ravenw.com/blog/2011/08/24/coroutine-part-1-defination-and-classification-of-corouti ...

  3. ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......

    今天用Hibernate建立外键的时候发现没有建立 但是创建了这个字段 情景: user表有一字段role,role是role表id字段的外键 原因: user表中已经有记录了,而且有的记录role这 ...

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

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

  5. 局域网ping Linux主机名

    1.Windows ping Linux主机名 #ubuntu 下 sudo apt-get install winbind #centos 下 yum install samba service n ...

  6. 15款加速 Web 开发的 JavaScript 框架

    JavaScript 可以通过多种方式来创建交互式的网站和 Web 应用程序.利用 JavaScript,可以让你移动 HTML 元素,创建各种各样的自定义动画,给你的访问者更好的终端用户体验. 对于 ...

  7. 小数5.2500四舍五入保留1位小数的java算法之一

    BigDecimal bd = new BigDecimal(5.2500); BigDecimal a = bd.setScale(1, BigDecimal.ROUND_HALF_UP); dou ...

  8. 什么是闭包(closure),为什么要用它?

    闭包是指有权访问另一个函数作用域中变量的函数,创建闭包的最常见的方式就是在一个函数内创建另一个函数,通过另一个函数访问这个函数的局部变量,利用闭包可以突破作用链域,将函数内部的变量和方法传递到外部. ...

  9. 【服务器】CentOS下部署运行NodeJs Web App

    NodeJs Web App测试完成后,要怎么部署呢?介绍两个不错的方案 已知以下情景: 我要为 「kenniu」这个项目做配置 它的入口文件在 「/path/to/entry.js」 运行的User ...

  10. WinForm-GridView

    前端: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CKXM.aspx. ...