winform實現(xiàn)可拖動的自定義Label控件
本文實例為大家分享了winform可拖動的自定義Label控件,供大家參考,具體內(nèi)容如下
效果預(yù)覽:
實現(xiàn)步驟如下:
(1)首先在項目上右擊選擇:添加->新建項,添加自定義控件
(2)自定義的一個Label讓它繼承LabelControl控件,LabelControl控件是DevExpress控件庫里面的一種,和Label控件差不多,想了解更多關(guān)于DevExpress控件,推薦到DevExpress控件論壇學(xué)習(xí):
public partial class LabelModule : LabelControl
(3)這個Label需要實現(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)移動時的方法。
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 事件時,鼠標(biāo)的坐標(biāo)值是相對于引發(fā)事件的控件的坐標(biāo)。一些與拖放操作相關(guā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); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#日期控件datetimepicker保存空值的三種方法
- winform dateTime數(shù)據(jù)類型轉(zhuǎn)換方法
- c# Winform自定義控件-儀表盤功能
- Winform控件Picture實現(xiàn)圖片拖拽顯示效果
- WinForm IP地址輸入框控件實現(xiàn)
- WinForm實現(xiàn)鼠標(biāo)拖動控件跟隨效果
- 使用重繪項美化WinForm的控件
- C# winform自定義翻頁控件詳解
- C# WinForm實現(xiàn)窗體上控件自由拖動功能示例
- Winform控件SaveFileDialog用于保存文件
- C#中WinForm控件的拖動和縮放的實現(xiàn)代碼
- C# WinForm-Timer控件的使用
相關(guān)文章
Unity通過腳本創(chuàng)建網(wǎng)格Mesh的方法
Unity中的網(wǎng)格作為組件不能脫離物體單獨存在,通過新建腳本來實現(xiàn)相關(guān)操作,本文重點給大家介紹Unity通過腳本創(chuàng)建網(wǎng)格Mesh的方法,感興趣的朋友一起看看吧2022-04-04Winform項目中使用FastReport.Net報表控件
這篇文章介紹了Winform項目中使用FastReport.Net報表控件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#中DataTable 轉(zhuǎn)換為 Json的方法匯總(三種方法)
JavaScript Object Notation (Json)是一種輕量級的數(shù)據(jù)交換格式,下面小編給大家介紹三種方法實現(xiàn)DataTable轉(zhuǎn)換成 Json 對象,感興趣的朋友一起看看吧2016-11-11Unity3D實現(xiàn)飛機(jī)大戰(zhàn)游戲(1)
這篇文章主要為大家詳細(xì)介紹了Unity3D實現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06