雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

winform實(shí)現(xiàn)可拖動(dòng)的自定義Label控件

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

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

效果預(yù)覽:

實(shí)現(xiàn)步驟如下:

(1)首先在項(xiàng)目上右擊選擇:添加->新建項(xiàng),添加自定義控件

(2)自定義的一個(gè)Label讓它繼承LabelControl控件,LabelControl控件是DevExpress控件庫(kù)里面的一種,和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# ListBox中的Item拖拽代碼分享

欄    目:C#教程

下一篇:C#利用GDI+畫圖的基礎(chǔ)實(shí)例教程

本文標(biāo)題:winform實(shí)現(xiàn)可拖動(dòng)的自定義Label控件

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5220.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有