winform實(shí)現(xiàn)可拖動(dòng)的自定義Label控件
本文實(shí)例為大家分享了winform可拖動(dòng)的自定義Label控件,供大家參考,具體內(nèi)容如下
效果預(yù)覽:

實(shí)現(xiàn)步驟如下:
(1)首先在項(xiàng)目上右擊選擇:添加->新建項(xiàng),添加自定義控件


(2)自定義的一個(gè)Label讓它繼承LabelControl控件,LabelControl控件是DevExpress控件庫里面的一種,和Label控件差不多,想了解更多關(guān)于DevExpress控件,推薦到DevExpress控件論壇學(xué)習(xí):
public partial class LabelModule : LabelControl
(3)這個(gè)Label需要實(shí)現(xiàn)的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,也就是鼠標(biāo)彈起的方法。
private void LabelModule_MouseUp(object sender, MouseEventArgs e)
{
IsMouseDown = false;
this.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
this.Cursor = Cursors.Default;
}
(5)MouseMove,也就是鼠標(biāo)移動(dòng)時(shí)的方法。
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 指的是:鼠標(biāo)的坐標(biāo)因所引發(fā)的事件而異。例如,當(dāng)處理 Control.MouseMove 事件時(shí),鼠標(biāo)的坐標(biāo)值是相對(duì)于引發(fā)事件的控件的坐標(biāo)。一些與拖放操作相關(guān)的事件具有相對(duì)于窗體原點(diǎn)或屏幕原點(diǎn)的關(guān)聯(lián)的鼠標(biāo)坐標(biāo)值。
完整代碼: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);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#日期控件datetimepicker保存空值的三種方法
- winform dateTime數(shù)據(jù)類型轉(zhuǎn)換方法
- c# Winform自定義控件-儀表盤功能
- Winform控件Picture實(shí)現(xiàn)圖片拖拽顯示效果
- WinForm IP地址輸入框控件實(shí)現(xiàn)
- WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果
- 使用重繪項(xiàng)美化WinForm的控件
- C# winform自定義翻頁控件詳解
- C# WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能示例
- Winform控件SaveFileDialog用于保存文件
- C#中WinForm控件的拖動(dòng)和縮放的實(shí)現(xiàn)代碼
- C# WinForm-Timer控件的使用
相關(guān)文章
Unity實(shí)現(xiàn)紅酒識(shí)別的示例代碼
本文主要介紹了如何通過Unity實(shí)現(xiàn)紅酒識(shí)別,可以實(shí)現(xiàn)識(shí)別圖像中的紅酒標(biāo)簽,返回紅酒名稱、國(guó)家、產(chǎn)區(qū)、酒莊、類型、糖分、葡萄品種、酒品描述等信息,感興趣的可以學(xué)習(xí)一下2022-02-02
Unity3D Shader實(shí)現(xiàn)貼圖切換效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)貼圖切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
小菜編程成長(zhǎng)記(一 面試受挫——代碼無錯(cuò)就是好?)
小菜編程成長(zhǎng)記(一 面試受挫——代碼無錯(cuò)就是好?)...2006-10-10
C# 重寫ComboBox實(shí)現(xiàn)下拉任意組件的方法
C#種的下拉框ComboBox不支持下拉復(fù)選框列表與下拉樹形列表等,系統(tǒng)中需要用到的地方使用了第三方組件,現(xiàn)在需要將第三方組件替換掉。這篇文章主要介紹了C# 重寫ComboBox實(shí)現(xiàn)下拉任意組件的相關(guān)資料,需要的朋友可以參考下2016-10-10

