如何用Winform制作支持拖动的自定义标签控件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计813个文字,预计阅读时间需要4分钟。
本文将分享如何创建一个可拖动的自定义Label控件,并提供相关参考资料和整体内容概览。以下是具体步骤和预览效果:
效果预览:+ 结果展示:一个可拖动的自定义Label控件,具有透明背景和自定义边框。+ 实现步骤:在WinForms项目中创建自定义控件,设置属性,实现拖动功能。+ 参考内容:详细步骤和代码示例。
实现步骤:
1.项目创建:在Visual Studio中,右键点击项目,选择添加 - 新建项,选择Windows窗体用户控件。
2.控件设计:
3.拖动功能:
- 为Label控件添加MouseMove事件处理程序。 - 在处理程序中,记录鼠标移动事件的位置,并更新Label控件的Position属性。代码示例:csharppublic partial class DraggableLabel : Label{ public DraggableLabel() { InitializeComponent(); }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); this.Location=new Point(Cursor.Position.X - this.Width / 2, Cursor.Position.Y - this.Height / 2); }}
通过以上步骤和代码示例,您可以在WinForms项目中创建一个可拖动的自定义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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计813个文字,预计阅读时间需要4分钟。
本文将分享如何创建一个可拖动的自定义Label控件,并提供相关参考资料和整体内容概览。以下是具体步骤和预览效果:
效果预览:+ 结果展示:一个可拖动的自定义Label控件,具有透明背景和自定义边框。+ 实现步骤:在WinForms项目中创建自定义控件,设置属性,实现拖动功能。+ 参考内容:详细步骤和代码示例。
实现步骤:
1.项目创建:在Visual Studio中,右键点击项目,选择添加 - 新建项,选择Windows窗体用户控件。
2.控件设计:
3.拖动功能:
- 为Label控件添加MouseMove事件处理程序。 - 在处理程序中,记录鼠标移动事件的位置,并更新Label控件的Position属性。代码示例:csharppublic partial class DraggableLabel : Label{ public DraggableLabel() { InitializeComponent(); }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); this.Location=new Point(Cursor.Position.X - this.Width / 2, Cursor.Position.Y - this.Height / 2); }}
通过以上步骤和代码示例,您可以在WinForms项目中创建一个可拖动的自定义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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

