winform实现可拖动的自定义Label控件

本文实例为大家分享了winform可拖动的自定义Label控件,供大家参考,具体内容如下

效果预览:

实现步骤如下:

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • winform实现拖动文件到窗体上的方法

    本文实例讲述了winform实现拖动文件到窗体上的方法.分享给大家供大家参考.具体如下: private void Form5_Load(object sender, EventArgs e) { this.AllowDrop=true; } private void Form5_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] files

  • WinForm实现鼠标拖动控件跟随效果

    本文实例为大家分享了WinForm实现鼠标拖动控件跟随效果的具体代码,供大家参考,具体内容如下 1. 运行初始窗口如下: 2. 拖动后效果如下: 3. 代码如下: public partial class Form1 : Form { /* * 理解了下面的几个概念,就能完全明白相对坐标的变化. * MouseEventArgs e 为事件鼠标参数,因此,e.Location 指示了位于事件源上的光标坐标 * Cursor.Position 获取的是相对于用户屏幕的光标坐标 * PointToC

  • C#中WinForm控件的拖动和缩放的实现代码

    C# WinForm控件的拖动和缩放是个很有用的功能.实现起来其实很简单的,主要是设计控件的MouseDown.MouseLeave.MouseMove事件,下面的几个步骤将逐步实现C# WinForm控件的拖动和缩放的功能. 1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition { MouseSizeNone = 0, //'无 MouseSizeRight = 1, //'拉伸右边框 MouseSizeLeft = 2, //'拉伸左边框

  • C# Winform 让整个窗口都可以拖动

    今天在网上查一些资料的时候, 无意中发现另一种办法, 非常方便, 调用系统的 API 来实现的, 效果也很好. 赶紧收藏了~ 复制代码 代码如下: [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int

  • C# WinForm实现窗体上控件自由拖动功能示例

    本文实例讲述了C# WinForm实现窗体上控件自由拖动功能.分享给大家供大家参考,具体如下: 说明:首先在窗体上放一个PictrueBox控件,命名为pb1,拖动完整代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usin

  • C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法

    本文实例讲述了C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法.分享给大家供大家参考,具体如下: winform 程序运行过程中,用户用鼠标拖动窗体大小时,如将窗体调整得极小,可能窗体上的控件就面目全非(或看不到了),用下面的代码可以设定窗口的最小尺寸,以防止这种情况 private void Form1_ResizeEnd(object sender, EventArgs e) { //this.Text = "2width:" + this.Width.ToStrin

  • winform实现可拖动的自定义Label控件

    本文实例为大家分享了winform可拖动的自定义Label控件,供大家参考,具体内容如下 效果预览: 实现步骤如下: (1)首先在项目上右击选择:添加->新建项,添加自定义控件 (2)自定义的一个Label让它继承LabelControl控件,LabelControl控件是DevExpress控件库里面的一种,和Label控件差不多,想了解更多关于DevExpress控件,推荐到DevExpress控件论坛学习: public partial class LabelModule : LabelC

  • Android通过自定义ImageView控件实现图片的缩放和拖动的实现代码

    概述:通过自定义ImageView控件,在xml布局里面调用自定的组件实现图片的缩放. /** * 自定义的ImageView控制,可对图片进行多点触控缩放和拖动 * * @author qiuwanyong */ public class MyImageView extends ImageView { /** * 初始化状态常量 */ public static final int STATUS_INIT = 1; /** * 图片放大状态常量 */ public static final i

  • ASP.NET中Literal与Label控件的区别

    一.方案和背景 Literal 控件表示用于向页面添加内容的几个选项之一.对于静态内容,无需使用容器,可以将标记作为 HTML 直接添加到页面中.但是,如果要动态添加内容,则必须将内容添加到容器中.典型的容器有 Label 控件.Literal 控件.Panel 控件和 PlaceHolder 控件. Literal 控件与 Label 控件的区别在于 Literal 控件不向文本中添加任何 HTML 元素.(Label 控件呈现一个 span 元素.)因此,Literal 控件不支持包括位置属

  • asp.net webform自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: <style type="text/css"> .pager-m-l { margin-left: 10px; } .pager { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; fo

  • Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解

    一.淘宝商品详情页效果 我们的效果 二.实现思路 使用两个scrollView,两个scrollView 竖直排列,通过自定义viewGroup来控制两个scrollView的竖直排列,以及滑动事件的处理.如下图 三.具体实现 1.继承viewGroup自定义布局View 重写onMeasure()和onLayout方法,在onLayout方法中完成对两个子ScrollView的竖直排列布局,代码如下: 布局文件: <RelativeLayout xmlns:android="http:/

  • Android开发之拖动条/滑动条控件、星级评分控件功能的实例代码

    ProgressBar有2个子控件: SeekBar   拖动条控件 RatingBar   星级评分控件 1.拖动条控件 <SeekBar android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/seekBar" android:min="0" android:max="100" andro

  • Vue自定义树形控件使用详解

    本文实例为大家分享了Vue自定义树形控件的使用方法,供大家参考,具体内容如下 效果图: 数据结构: tree: { title: '', // 标题(姓名) key: '0', head: '', // 头像 selectStatus: false, // checkBox选中状态 children: [ { title: '旺旺一部', key: '0-0', head: '', selectStatus: false, children: [ { key: '0-0-0', title: '

  • iOS开发中使用Quartz2D绘图及自定义UIImageView控件

    绘制基本图形 一.简单说明 图形上下文(Graphics Context):是一个CGContextRef类型的数据 图形上下文的作用:保存绘图信息.绘图状态 决定绘制的输出目标(绘制到什么地方去?)(输出目标可以是PDF文件.Bitmap或者显示器的窗口上) 相同的一套绘图序列,指定不同的Graphics Context,就可将相同的图像绘制到不同的目标上. Quartz2D提供了以下几种类型的Graphics Context: Bitmap Graphics Context PDF Grap

  • 自定义ExtJS控件之下拉树和下拉表格附源码

    简介 在Ext官方的例子中只有下拉列表控件,但是在实际业务中只有下拉列表无法满足需求的,像下拉树和下拉表格都是很常见的控件,对于刚使用Ext的人来说,自定义一个控件好难,其实多读官方的源码有些事情就不会那么难了.下面是下拉树的代码: 复制代码 代码如下: Ext.define('ComboTreeBox',{ extend : 'Ext.form.field.ComboBox', multiSelect : true, createPicker : function(){ var me = th

  • android之视频播放系统VideoView和自定义VideoView控件的应用

    Android播放视频,包含系统自带VideoView控件,和自定义VideoView控件,可全屏播放,案例包含了本地视频和网络视频. 1:自定义VideoView控件 2:布局代码 3:Activity代码: 4:网络权限 5:效果图 小结:其中的Uri mUri = Uri.parse("android.resource://" + getPackageName() +"/"+ R.raw.qiche);//本地视频 是加载的本地视频,可以下载一个视频,在res

随机推荐