Winform 自定义Panel

来源:互联网 发布:口算题出题系统 mac 编辑:程序博客网 时间:2024/06/16 05:53
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TomWinform.CustomerControl
{
    public partial class DragPanel : Panel
    {
        private Color borderColor = Color.Black;
        private float borderSize = 1;
        private Point point;

        public DragPanel()
        {
            InitializeComponent();
            this.MouseDown += new MouseEventHandler(DragMouseDown);
            this.MouseMove += new MouseEventHandler(DragMouseMove);
            this.MouseUp += new MouseEventHandler(DragMouseUp);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            DrawBorder(pe);
            base.OnPaint(pe);
        }

        private void DragMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y > 0 && e.Y < 50)
            {
                this.Cursor = Cursors.SizeAll;
                point.X = e.X;
                point.Y = e.Y;
            }
        }

        private void DragMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Y > 0 && e.Y < 50)
                {
                    this.Left = (e.X - point.X) + this.Left;
                    this.Top = (e.Y - point.Y) + this.Top;
                }
            }
        }

        private void DragMouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        //画边框
        private void DrawBorder(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            Pen pen = new Pen(borderColor, borderSize);
            Point[] point = new Point[4];
            point[0] = new Point(0, 0);
            point[1] = new Point(this.Width - 1, 0);
            point[2] = new Point(this.Width - 1, this.Height - 1);
            point[3] = new Point(0, this.Height - 1);
            g.DrawPolygon(pen, point);
        }

        #region 属性
        [Description("边框颜色"), Category("自定义属性")]
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; this.Invalidate(); }
        }
        [Description("边框宽度"), Category("自定义属性")]
        public float BorderSize
        {
            get { return borderSize; }
            set { borderSize = value; this.Invalidate(); }
        }
        #endregion



    }
}