码迷,mamicode.com
首页 > Windows程序 > 详细

winform 可拖动的自定义Label控件

时间:2015-08-14 08:45:12      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:winform   自定义控件   

效果预览:

技术分享

实现步骤如下:

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

技术分享

技术分享

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

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


完整代码: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);
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

winform 可拖动的自定义Label控件

标签:winform   自定义控件   

原文地址:http://blog.csdn.net/a1061747415/article/details/47656307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!