【自定义UI】三、LemonUI

自定义ui,form基类

私有成员

#region[===== 私有成员 =====]
        /// <summary>
        /// 是否显示窗体圆角
        /// </summary>
        //private bool _showFormRadius = false;
        /// <summary>
        /// 是否显示边框
        /// </summary>
        private bool _showFormBorder = true;
        /// <summary>
        /// 是否显示标题栏
        /// </summary>
        private bool _showTitle = true;
        /// <summary>
        /// 是否显示关闭["X"]按钮
        /// </summary>
        private bool _controlBox = true;
        /// <summary>
        /// 是否显示最大化["口"]按钮
        /// </summary>
        private bool _maximizeBox = true;
        /// <summary>
        /// 是否显示最小化["——"]按钮
        /// </summary>
        private bool _minimizeBox = true;
        /// <summary>
        /// 是否显示扩展["\/"]按钮
        /// </summary>
        private bool _extendBox = false;
        /// <summary>
        /// 是否显示标题栏图标
        /// </summary>
        private bool _showTitleIcon = false;
        /// <summary>
        /// 鼠标事件使用,鼠标移动到矩形控制框"X"
        /// </summary>
        private bool _inControlBox = false;
        /// <summary>
        /// 鼠标事件使用,鼠标移动最大化矩形控制框
        /// </summary>
        private bool _inMaxBox = false;
        /// <summary>
        /// 鼠标事件使用,鼠标移动最小化矩形控制框
        /// </summary>
        private bool _inMinBox = false;
        /// <summary>
        /// 鼠标事件使用,鼠标移动到扩展矩形控制框
        /// </summary>
        private bool _inExtendBox = false;
        /// <summary>
        /// 是否窗体移动
        /// </summary>
        private bool _formMoveMouseDown = false;
        /// <summary>
        /// 最大化是否铺满屏幕
        /// </summary>
        private bool _isFullScreen = false;
        /// <summary>
        /// 标题栏高度
        /// </summary>
        private int _titleHeight = 36;
        /// <summary>
        /// 矩形控制框边长
        /// </summary>
        private readonly int _controlboxSide = 28;
        /// <summary>
        /// 矩形控制框左侧
        /// </summary>
        private int _controlBoxLeft = 0;
        /// <summary>
        /// 窗体边框颜色
        /// </summary>
        private Color _formBorderColor = Color.Black;
        /// <summary>
        /// 窗体的位置
        /// </summary>
        private Point _formLocation;
        /// <summary>
        /// 鼠标的位置
        /// </summary>
        private Point _mouseLocation;
        /// <summary>
        /// 最大化窗体前窗体的尺寸
        /// </summary>
        private Size _beforeMaximizeSize;
        /// <summary>
        /// 最大化窗体前窗体的位置
        /// </summary>
        private Point _beforeMaximizeLocation;
        /// <summary>
        /// 标题栏背景颜色
        /// </summary>
        private Color _titleBackColor = Color.FromArgb(100, 204, 255);
        /// <summary>
        /// 标题栏前景颜色
        /// </summary>
        private Color _titleForeColor = Color.White;
        /// <summary>
        /// 标题栏边框颜色
        /// </summary>
        private Color _titleBorderColor = Color.FromArgb(38, 38, 38);
        /// <summary>
        /// 鼠标移动到矩形控制框上的高亮颜色
        /// </summary>
        private Color _controlBoxFillHoverColor = Color.FromArgb(155, 121, 121, 121);
        /// <summary>
        /// 鼠标移动到关闭按钮上的高亮颜色
        /// </summary>
        private Color _closeBoxFillHoverColor = Color.Red;
        /// <summary>
        /// 标题字体
        /// </summary>
        private Font _titleFont = new Font("微软雅黑", 10);
        /// <summary>
        /// 标题文字位置
        /// </summary>
        private StringAlignment _textAlignment = StringAlignment.Near;
        /// <summary>
        /// 控制框前景颜色
        /// </summary>
        private Color _controlBoxForeColor = Color.White;
        /// <summary>
        /// 关闭按钮矩形控制框
        /// </summary>
        private Rectangle _controlBoxRect;
        /// <summary>
        /// 最大化按钮矩形控制框
        /// </summary>
        private Rectangle _maximizeBoxRect;
        /// <summary>
        /// 最小化按钮矩形控制框
        /// </summary>
        private Rectangle _minimizeBoxRect;
        /// <summary>
        /// 扩展按钮矩形控制框
        /// </summary>
        private Rectangle _extendBoxRect;
        #endregion[===== 私有成员 =====]

重定义属性

#region[===== 重定义属性 =====]
        /// <summary>
        /// 是否多文档页面
        /// </summary>
        [Browsable(false)]
        public new bool IsMdiContainer
        {
            get => base.IsMdiContainer;
            set => base.IsMdiContainer = false;
        }
        /// <summary>
        /// 是否启用自动滚动
        /// </summary>
        [Browsable(false)]
        public new bool AutoScroll
        {
            get => base.AutoScroll;
            set => base.AutoScroll = false;
        }
        /// <summary>
        /// 窗体边框样式
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]//设置编译器类无法查看,不允许修改
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]//告诉编译器,此方法不生成代码
        public new FormBorderStyle FormBorderStyle
        {
            get => base.FormBorderStyle;
            set
            {
                if (!Enum.IsDefined(typeof(FormBorderStyle), value))
                    throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(FormBorderStyle));//若修改,抛出异常
                base.FormBorderStyle = FormBorderStyle.None;//设置无边框
            }
        }
        /// <summary>
        /// 是否显示控制框按钮
        /// </summary>
        [Description("是否显示控制框按钮"), Category("WindowStyle")]
        [DefaultValue(true)]
        public new bool ControlBox
        {
            get => this._controlBox;
            set
            {
                this._controlBox = value;
                if (!value) this._maximizeBox = this._minimizeBox = false;
                CalcSystemBoxPosition();
                Invalidate();
            }
        }
        /// <summary>
        /// 是否显示窗体的最大化按钮
        /// </summary>
        [DefaultValue(true)]
        [Description("是否显示窗体的最大化按钮"), Category("WindowStyle")]
        public new bool MaximizeBox
        {
            get => this._maximizeBox;
            set
            {
                this._maximizeBox = value;
                if (value) this._minimizeBox = true;
                CalcSystemBoxPosition();
                Invalidate();
            }
        }
        /// <summary>
        /// 是否显示窗体的最小化按钮
        /// </summary>
        [DefaultValue(true)]
        [Description("是否显示窗体的最小化按钮"), Category("WindowStyle")]
        public new bool MinimizeBox
        {
            get => this._minimizeBox;
            set
            {
                this._minimizeBox = value;
                if (!value) this._maximizeBox = false;
                CalcSystemBoxPosition();
                Invalidate();
            }
        }
        #endregion[===== 重定义属性 =====]

自定义属性

#region[===== 自定义属性 =====]
        /// <summary>
        /// 是否显示窗体边框
        /// </summary>
        [DefaultValue(true)]
        [Description("是否显示窗体边框"), Category("WindowStyle")]
        public bool ShowFormBorder
        {
            get => this._showFormBorder;
            set
            {
                this._showFormBorder = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 是否显示窗体圆角
        /// </summary>
        /*[DefaultValue(false)]
        [Description("是否显示窗体圆角"), Category("WindowStyle")]
        public bool ShowFormRadius
        {
            get => this._showFormRadius;
            set
            {
                this._showFormRadius = value;
                Invalidate();
            }
        }*/
        /// <summary>
        /// 是否显示标题栏
        /// </summary>
        [DefaultValue(true)]
        [Description("是否显示标题栏"), Category("WindowStyle")]
        public bool ShowTitle
        {
            get => this._showTitle;
            set
            {
                this._showTitle = value;
                Padding = new Padding(Padding.Left, value ? this._titleHeight : 0, Padding.Right, Padding.Bottom);
                Invalidate();
            }
        }
        /// <summary>
        /// 标题栏高度
        /// </summary>
        [DefaultValue(36)]
        [Description("标题栏高度"), Category("LemonUI")]
        public int TitleHeight
        {
            get => this._titleHeight;
            set
            {
                this._titleHeight = Math.Max(value, this._controlboxSide + 2);
                Padding = new Padding(Padding.Left, this._showTitle ? this._titleHeight : Padding.Top, Padding.Right, Padding.Bottom);
                CalcSystemBoxPosition();
                Invalidate();
            }
        }
        /// <summary>
        /// 窗体边框颜色
        /// </summary>
        [Description("窗体边框颜色"), Category("LemonUI")]
        public Color FormBorderColor
        {
            get => this._formBorderColor;
            set
            {
                this._formBorderColor = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 标题栏背景颜色
        /// </summary>
        [DefaultValue(typeof(Color), "153, 204, 255")]
        [Description("标题栏背景颜色"), Category("LemonUI")]
        public Color TitleBackColor
        {
            get => this._titleBackColor;
            set
            {
                if (this._titleBackColor != value)
                {
                    this._titleBackColor = value;
                    Invalidate();
                }
            }
        }
        /// <summary>
        /// 标题栏前景颜色
        /// </summary>
        [DefaultValue(typeof(Color), "White")]
        [Description("标题栏前景颜色"), Category("LemonUI")]
        public Color TitleForeColor
        {
            get => this._titleForeColor;
            set
            {
                if (this._titleForeColor != value)
                {
                    this._titleForeColor = value;
                    Invalidate();
                }
            }
        }
        /// <summary>
        /// 标题栏边框颜色
        /// </summary>
        [DefaultValue(typeof(Color), "White")]
        [Description("标题栏边框颜色"), Category("LemonUI")]
        public Color TitleBorderColor
        {
            get => this._titleBorderColor;
            set
            {
                if (this._titleBorderColor != value)
                {
                    this._titleBorderColor = value;
                    Invalidate();
                }
            }
        }
        /// <summary>
        /// 鼠标移动到矩形控制框上的高亮颜色
        /// </summary>
        [DefaultValue(typeof(Color), "(120, 225, 225, 225)")]
        [Description("鼠标移动到矩形控制框上的高亮颜色"), Category("WindowStyle")]
        public Color ControlBoxFillHoverColor
        {
            get => this._controlBoxFillHoverColor;
            set => this._controlBoxFillHoverColor = value;
        }
        /// <summary>
        /// 鼠标移动到关闭按钮上的高亮颜色
        /// </summary>
        [DefaultValue(typeof(Color), "Red")]
        [Description("鼠标移动到关闭按钮上的高亮颜色"), Category("WindowStyle")]
        public Color CloseBoxFillHoverColor
        {
            get => this._closeBoxFillHoverColor;
            set => this._closeBoxFillHoverColor = value;
        }
        /// <summary>
        /// 标题字体
        /// </summary>
        [DefaultValue(typeof(Font), "微软雅黑, 10")]
        [Description("标题字体"), Category("LemonUI")]
        public Font TitleFont
        {
            get => this._titleFont;
            set
            {
                _titleFont = value;
                Invalidate();
            }
        }
        [Description("标题文字对齐方式"), Category("LemonUI")]
        public StringAlignment TextAlignment
        {
            get => this._textAlignment;
            set
            {
                this._textAlignment = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 控制框前景颜色
        /// </summary>
        [DefaultValue(typeof(Color), "White")]
        [Description("控制框前景颜色"), Category("LemonUI")]
        public Color ControlBoxForeColor
        {
            get => this._controlBoxForeColor;
            set
            {
                if (this.ControlBoxForeColor != value)
                {
                    this._controlBoxForeColor = value;
                    Invalidate();
                }
            }
        }
        /// <summary>
        /// 是否显示扩展框按钮
        /// </summary>
        [DefaultValue(false)]
        [Description("显示扩展按钮"), Category("LemonUI")]
        public bool ExtendBox
        {
            get => this._extendBox;
            set
            {
                this._extendBox = value;
                CalcSystemBoxPosition();
                Invalidate();
            }
        }
        [DefaultValue(false)]
        [Description("显示标题栏图标"), Category("WindowStyle")]
        public bool ShowTitleIcon
        {
            get => this._showTitleIcon;
            set
            {
                this._showTitleIcon = value;
                Invalidate();
            }
        }
        /// <summary>
        /// 最大化是否铺满屏幕
        /// </summary>
        [Description("最大化是否铺满屏幕"), Category("WindowStyle")]
        public bool IsFullScreen
        {
            get => this._isFullScreen;
            set
            {
                this._isFullScreen = value;
                /**
                 * Bounds.Size 全屏铺满
                 * WorkingArea.Size 去除任务栏、 停靠的窗口和已停靠的工具条的桌面区域
                 */
                base.MaximumSize = this._isFullScreen ? Screen.PrimaryScreen.Bounds.Size : Screen.PrimaryScreen.WorkingArea.Size;
            }
        }
        #endregion[===== 自定义属性 =====]

私有函数

#region[===== 私有函数 =====]
        /// <summary>
        /// 计算系统框坐标
        /// </summary>
        private void CalcSystemBoxPosition()
        {
            this._controlBoxLeft = this.Width;
            if (this._controlBox)//显示控制框
            {
                //修改控制框的大小 = (X:整体宽度 - 边距- 控制框宽度,Y:标题栏高度/2 - 控制框大小/2,W:控制框宽,H:控制框高)
                _controlBoxRect = new Rectangle(this.Width - 6 - this._controlboxSide, (this._titleHeight - this._controlboxSide) / 2, this._controlboxSide, this._controlboxSide);
                if (this._maximizeBox)//显示最大化按钮
                {
                    //最大化矩形控制框大小 = (X:矩形控制框左边距 - 控制框大小 - 2,Y:矩形控制框上边距,W:控制框宽,H:控制框高)
                    this._maximizeBoxRect = new Rectangle(this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
                    this._controlBoxLeft = this._maximizeBoxRect.Left;
                }
                else
                {
                    //最大化矩形控制框大小 = (X:窗体宽度 + 1,Y:窗体高度 + 1,W:1,H:1),隐藏在窗体左下角
                    this._maximizeBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
                }

                if (this._minimizeBox)//显示最小化按钮
                {
                    //最小化矩形控制框大小 = (X:显示最大化按钮?最大化控制框左边距 - 控制框大小 - 2 : 矩形控制框左边距 - 控制框大小 - 2,Y:矩形控制框上边距,W:控制框宽,H:控制框高)
                    this._minimizeBoxRect = new Rectangle(this._maximizeBox ? this._maximizeBoxRect.Left - this._controlboxSide - 2 : this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
                    this._controlBoxLeft = this._minimizeBoxRect.Left;
                }
                else
                {
                    //最小化矩形控制框大小 = (X:窗体宽度 + 1,Y:窗体高度 + 1,W:1,H:1),隐藏在窗体左下角
                    this._minimizeBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
                }

                if (this._extendBox)//显示扩展框按钮
                {
                    if (this._minimizeBox)//显示最小化按钮
                    {
                        //扩展矩形框按钮大小 = (X:最小化矩形控制框左边距 - 控制框大小 - 2,Y:矩形控制框上边距,W:控制框宽,H:控制框高)
                        this._extendBoxRect = new Rectangle(this._minimizeBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
                        this._controlBoxLeft = this._extendBoxRect.Left;
                    }
                    else
                    {
                        //扩展矩形框按钮大小 = (X:矩形控制框左边距 - 控制框大小 - 2,Y:矩形控制框上边距,W:控制框宽,H:控制框高)
                        this._extendBoxRect = new Rectangle(this._controlBoxRect.Left - this._controlboxSide - 2, this._controlBoxRect.Top, this._controlboxSide, this._controlboxSide);
                    }
                }
            }
            else
            {
                //扩展矩形框按钮大小 = 最大矩形控制框大小 = 最小矩形控制框大小 = 矩形控制框大小 = (X:窗体宽度 + 1,Y:窗体高度 + 1,W:1,H:1),隐藏在窗体左下角
                this._extendBoxRect = this._maximizeBoxRect = this._minimizeBoxRect = this._controlBoxRect = new Rectangle(this.Width + 1, this.Height + 1, 1, 1);
            }
        }
        /// <summary>
        /// 显示窗口最大化
        /// </summary>
        /// <param name="isOnMoving"></param>
        private void ShowMaximizeForm(bool isOnMoving = false)
        {
            Screen screen = Screen.FromPoint(MousePosition);
            base.MaximumSize = this._isFullScreen ? screen.Bounds.Size : screen.WorkingArea.Size;
            if (screen.Primary)
            {
                this.MaximizedBounds = this._isFullScreen ? screen.Bounds : screen.WorkingArea;
            }
            else
            {
                MaximizedBounds = new Rectangle(0, 0, 0, 0);
            }
            if (WindowState == FormWindowState.Normal)
            {
                this._beforeMaximizeSize = this.Size;
                this._beforeMaximizeLocation = isOnMoving ? _formLocation : this.Location;
                WindowState = FormWindowState.Maximized;
            }
            else if (WindowState == FormWindowState.Maximized)
            {
                if (this._beforeMaximizeSize.Width == 0 || this._beforeMaximizeSize.Height == 0)
                {
                    this._beforeMaximizeSize.Width = MinimumSize.Width > 0 ? MinimumSize.Width : 800;
                    this._beforeMaximizeSize.Height = MinimumSize.Height > 0 ? MinimumSize.Height : 600;
                }
                this.Size = this._beforeMaximizeSize;
                if (_beforeMaximizeLocation.X == 0 || this._beforeMaximizeLocation.Y == 0)
                {
                    this._beforeMaximizeLocation.X = screen.Bounds.Left + screen.Bounds.Width / 2;
                    this._beforeMaximizeLocation.Y = screen.Bounds.Top + screen.Bounds.Height / 2;
                }
                this.Location = this._beforeMaximizeLocation;
                WindowState = FormWindowState.Normal;
            }
            Invalidate();
        }
        #endregion[===== 私有函数 =====]

重写函数

#region[===== 重写函数 ======]
        /// <summary>
        /// 页面绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (this.Width <= 0 || this.Height <= 0 || this.FormBorderStyle != FormBorderStyle.None) return;
            Graphics g = e.Graphics;

            ///中心点
            Point centerPoint = new Point(0, 0);
            if (this._showTitle)//显示标题栏
            {

                /*绘制标题栏*/
                g.FillRectangle(this._titleBackColor, 0, 0, this.Width, this._titleHeight);
                //绘制标题栏下面那根线
                g.DrawLine(this.TitleBorderColor, 0, this._titleHeight, this.Width, this._titleHeight);
                /**
                 * 绘制标题图标
                 */
                if (this._showTitleIcon && this.Icon != null)
                {
                    using (Image image = this.Icon.IconToImage())
                    {
                        g.DrawIcon(this.Icon, new Rectangle(5, (this._titleHeight - 20) / 2, 20, 20));
                    }
                }
                /**
                 * 绘制标题文字
                 * 判断是否填写标题文字
                 * 测量:使用标题字体,标题字的大小 
                 * 根据位置进行绘制
                 */
                if (!string.IsNullOrEmpty(this.Text))
                {
                    SizeF sf = e.Graphics.MeasureString(this.Text, this._titleFont);
                    PointF point = new Point(0, 0);
                    if (this._textAlignment == StringAlignment.Center)
                    {
                        point.X = (this.Width - sf.Width) / 2;
                    }
                    else if (this._textAlignment == StringAlignment.Far)
                    {
                        point.X = (this._controlBoxLeft - sf.Width - 5);
                    }
                    else
                    {
                        point.X = ((this._showTitleIcon && this.Icon != null) ? 20 : 0) + 5;
                    }
                    point.Y = (this.TitleHeight - sf.Height) / 2;

                    g.DrawString(this.Text, this._titleFont, this._titleForeColor, point);
                }
                /**
                 * 绘制扩展按钮
                 */
                if (this._extendBox)
                {

                    centerPoint.X = this._extendBoxRect.Left + this._extendBoxRect.Width / 2;
                    centerPoint.Y = this._extendBoxRect.Top + this._extendBoxRect.Height / 2;
                    if (this._inExtendBox)
                    {
                        g.FillRectangle(this.ControlBoxFillHoverColor, this._extendBoxRect);
                    }
                    /**
                     * *     *
                     *  *   *
                     *   * *
                     *    *
                     */
                    List<Point> points = new List<Point>
                    {
                        new Point(centerPoint.X - 5,centerPoint.Y - 2),
                        new Point(centerPoint.X,centerPoint.Y + 3),
                        new Point(centerPoint.X + 5,centerPoint.Y - 2)
                    };
                    g.DrawLines(this.ControlBoxForeColor, points.ToArray());
                }

                /*
                 * 显示最小化"——"按钮
                 */
                if (this._minimizeBox)
                {
                    centerPoint.X = this._minimizeBoxRect.Left + this._minimizeBoxRect.Width / 2;
                    centerPoint.Y = this._minimizeBoxRect.Top + this._minimizeBoxRect.Height / 2;
                    if (this._inMinBox)
                    {
                        g.FillRectangle(this.ControlBoxFillHoverColor, this._minimizeBoxRect);
                    }
                    /**
                     * 
                     * **********
                     * 
                     */
                    /* 获取矩形框中心点 */
                    g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y, centerPoint.X + 6, centerPoint.Y);
                }
                /*
                 * 显示最大化按钮
                 */
                if (this._maximizeBox)
                {
                    /* 获取矩形框中心点 */
                    centerPoint.X = this._maximizeBoxRect.Left + this._maximizeBoxRect.Width / 2;
                    centerPoint.Y = this._maximizeBoxRect.Top + this._maximizeBoxRect.Height / 2;
                    if (this._inMaxBox)
                    {
                        g.FillRectangle(this.ControlBoxFillHoverColor, this._maximizeBoxRect);
                    }
                    /**窗体处于最大化时
                     * 先绘制宽高为7的矩形在左下角
                     * 再绘制四条线段围绕右上角
                     * 便是最大化后的按钮
                     */
                    if (WindowState == FormWindowState.Maximized)
                    {
                        /**
                         *     * * * * *
                         *   * * * * * *
                         *   *       * *
                         *   *       * *
                         *   *       * *
                         *   * * * * *
                         */
                        /* 绘制左下角矩形 */
                        g.DrawRectangle(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 3, 8, 8);
                        /* 绘制右上角矩形 */
                        List<Point> points = new List<Point>
                        {
                            new Point(centerPoint.X - 3, centerPoint.Y - 3),
                            new Point(centerPoint.X - 3, centerPoint.Y - 6),
                            new Point(centerPoint.X + 6, centerPoint.Y - 6),
                            new Point(centerPoint.X + 6, centerPoint.Y + 3),
                            new Point(centerPoint.X + 3, centerPoint.Y + 3),
                        };
                        g.DrawLines(this.ControlBoxForeColor, points.ToArray());

                    }
                    if (WindowState == FormWindowState.Normal)
                    {
                        g.DrawRectangle(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 6, 12, 12);
                    }
                }
                /*
                 * 显示“X”控制框按钮
                 */
                if (this._controlBox)
                {
                    centerPoint.X = this._controlBoxRect.Left + this._controlBoxRect.Width / 2;
                    centerPoint.Y = this._controlBoxRect.Top + this._controlBoxRect.Height / 2;
                    if (this._inControlBox)
                    {
                        g.FillRectangle(this.CloseBoxFillHoverColor, this._controlBoxRect);
                    }
                    /**
                     *   *        *
                     *     *   *   
                     *       * 
                     *     *   *   
                     *   *        *
                     */
                    g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y - 6, centerPoint.X + 6, centerPoint.Y + 6);
                    g.DrawLine(this._controlBoxForeColor, centerPoint.X - 6, centerPoint.Y + 6, centerPoint.X + 6, centerPoint.Y - 6);
                }
                /**
                 * 绘制边框
                 */
                if (this._showFormBorder)
                {
                    g.DrawRectangle(this._formBorderColor, 0, 0, this.Width - 1, this.Height - 1);
                }
            }
        }
        /// <summary>
        /// 页面尺寸变化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            CalcSystemBoxPosition();
        }
        /// <summary>
        /// 窗体首次显示事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            CalcSystemBoxPosition();
        }

        #region[===== 鼠标事件重写 =====]
        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (_formMoveMouseDown && !MousePosition.Equals(_mouseLocation))
            {
                if (WindowState == FormWindowState.Maximized)
                {
                    int maximizeWidth = this.Width;
                    int locationX = this.Left;
                    ShowMaximizeForm();

                    //计算缩放比例,鼠标和原来的位置的相对位置差
                    float offsetRatio = 1 - (float)this.Width / maximizeWidth;
                    this._mouseLocation.X -= (int)((_mouseLocation.X - locationX) * offsetRatio);
                }
                int offsetX = this._mouseLocation.X - MousePosition.X;
                int offsetY = this._mouseLocation.Y - MousePosition.Y;

                this.Location = new Point(this._formLocation.X - offsetX, this._formLocation.Y - offsetY);
            }
            else
            {
                if (this.FormBorderStyle == FormBorderStyle.None)
                {
                    bool incontrolBox = e.Location.PointInRect(this._controlBoxRect);
                    if (WindowState == FormWindowState.Maximized && this._controlBox)
                    {
                        if (e.Location.X > this._controlBoxRect.Left && e.Location.Y < this._titleHeight) incontrolBox = true;
                    }
                    bool inMaxBox = e.Location.PointInRect(this._maximizeBoxRect);
                    bool inMinBox = e.Location.PointInRect(this._minimizeBoxRect);
                    bool inExtendBox = e.Location.PointInRect(this._extendBoxRect);
                    bool isChange = false;
                    if (incontrolBox != this._inControlBox)
                    {
                        this._inControlBox = incontrolBox;
                        isChange = true;
                    }
                    if (inMaxBox != this._inMaxBox)
                    {
                        this._inMaxBox = inMaxBox;
                        isChange = true;
                    }
                    if (inMinBox != this._inMinBox)
                    {
                        this._inMinBox = inMinBox;
                        isChange = true;
                    }
                    if (inExtendBox != this._inExtendBox)
                    {
                        this._inExtendBox = inExtendBox;
                        isChange = true;
                    }
                    if (isChange) Invalidate();
                }
                else
                {
                    this._inExtendBox = this._inMinBox = this._inMaxBox = this._inControlBox = false;
                }
            }

            base.OnMouseMove(e);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this._inExtendBox = this._inMinBox = this._inMaxBox = this._inControlBox = false;
            Invalidate();
        }
        /// <summary>
        /// 鼠标抬起事件
        /// </summary>
        /// <param name="e">鼠标参数</param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            if (!IsDisposed && this._formMoveMouseDown)
            {
                Screen screen = Screen.FromPoint(MousePosition);
                if (MousePosition.Y == screen.WorkingArea.Top && this._maximizeBox)
                {
                    ShowMaximizeForm(true);
                }

                // 防止窗体上移时标题栏超出容器,导致后续无法移动
                if (this.Top < screen.WorkingArea.Top)
                {
                    this.Top = screen.WorkingArea.Top;
                }

                // 防止窗体下移时标题栏超出容器,导致后续无法移动
                if (this.Top > screen.WorkingArea.Bottom - this._titleHeight)
                {
                    this.Top = screen.WorkingArea.Bottom - this._titleHeight;
                }
            }

            /*鼠标抬起,关闭窗体跟随鼠标移动*/
            this._formMoveMouseDown = false;
        }
        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (this._inControlBox || this._inMaxBox || this._inMinBox || this._inExtendBox || !this._showTitle || e.Y > Padding.Top) return;
            if (e.Button == MouseButtons.Left)
            {
                this._formMoveMouseDown = true;
                this._formLocation = this.Location;
                this._mouseLocation = MousePosition;
            }
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (FormBorderStyle == FormBorderStyle.None && this._showTitle)
            {
                if (this._inControlBox)
                {
                    this._inControlBox = false;
                    this.Close();
                }
                if (this._inMaxBox)
                {
                    this._inMaxBox = false;
                    ShowMaximizeForm();
                }
                if (this._inMinBox)
                {
                    this._inMinBox = false;
                    WindowState = FormWindowState.Minimized;
                }
                if (this._inExtendBox)
                {
                    this._inExtendBox = false;
                    MessageBox.Show("点击了扩展按钮矩形框!");
                }
            }
            base.OnMouseClick(e);
        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (!this.MaximizeBox) return;
            if (this._inControlBox || this._inMaxBox || this._inMaxBox || this._inExtendBox) return;
            if (e.Y > Padding.Top) return;
            ShowMaximizeForm();
        }
        #endregion[===== 鼠标事件重写 =====]

        #endregion[===== 重写函数 ======]

其他

		/// <summary>
		/// 唯一标识符
		/// </summary>
		public readonly Guid Guid = Guid.NewGuid();
        /// <summary>
        /// 无参构造函数
        /// </summary>
        public LemonForm()
        {
            base.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;//设置基础页面的最大尺寸
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;//设置窗体不显示边框

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值