自绘无标题栏Frame

先贴出来源码类和示例工程下载地址:戳我去下载

这个扩展类基于网络中的DForm修改,优化了一些内容,保证可用,可在留言或私信询问疑问。

其中一些选项可以看示例工程的设置,当然首先要把boderstyle设置为None,其他元素Dock位置可要通过窗体padding属性来控制。



关键为继承OnPaint函数,比传统直接派生winproc更加易懂但也无法随意的定制窗体形状,不过用graphicpath应该也可以。

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //draw title back
            DrawExTitleBack(e.Graphics);
            //draw title logo
            DrawExLogo(e.Graphics);
            //draw title text
            DrawExText(e.Graphics);
            //draw controlbox
            DrawExControlBox(e.Graphics);
            //draw border
            DrawExBoder(e.Graphics);
        }


还有标题栏按钮的状态监测:

protected override void OnMouseMove(MouseEventArgs e)
        {
            if (_MinButtonRectangle != Rectangle.Empty && _MinButtonRectangle.Contains(e.Location))
            {
                _MinButtonHover = true;

                this.Invalidate(_MinButtonRectangle);
            }
            else if (_MinButtonRectangle != Rectangle.Empty && _MinButtonRectangle.Contains(e.Location) == false && _MinButtonHover == true)
            {
                this.Invalidate(_MinButtonRectangle);
                _MinButtonHover = false;
            }

            if (_MaxButtonRetangle != Rectangle.Empty && _MaxButtonRetangle.Contains(e.Location))
            {
                _MaxButtonHover = true;

                this.Invalidate(_MaxButtonRetangle);
            }
            else if (_MaxButtonRetangle != Rectangle.Empty && _MaxButtonRetangle.Contains(e.Location) == false && _MaxButtonHover == true)
            {
                this.Invalidate(_MaxButtonRetangle);
                _MaxButtonHover = false;
            }

            if (_CloseButtonRectangle != Rectangle.Empty && _CloseButtonRectangle.Contains(e.Location))
            {
                _CloseButtonHover = true;

                this.Invalidate(_CloseButtonRectangle);
            }
            else if (_CloseButtonRectangle != Rectangle.Empty && _CloseButtonRectangle.Contains(e.Location) == false && _CloseButtonHover == true)
            {
                this.Invalidate(_CloseButtonRectangle);
                _CloseButtonHover = false;
            }

            if(_HelpButtonRectangle != Rectangle.Empty && _HelpButtonRectangle.Contains(e.Location))
            {
                _HelpButtonHover = true;
                this.Invalidate(_HelpButtonRectangle);
            }
            else if(_HelpButtonRectangle != Rectangle.Empty && _HelpButtonRectangle.Contains(e.Location) == false && _HelpButtonHover == true)
            {
                this.Invalidate(_HelpButtonRectangle);

                _HelpButtonHover = false;
            }

            if (_MaxButtonHover == true || _CloseButtonHover == true || _MinButtonHover == true || _HelpButtonHover == true)
                this.Cursor = Cursors.Hand;
            else
            {
                this.Cursor = Cursors.Default;
            }


            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if(_MinButtonHover == true)
            {
                _MinButtonHover = false;
                this.Invalidate(_MinButtonRectangle);
            }

            if(_MaxButtonHover == true)
            {
                _MaxButtonHover = false;
                this.Invalidate(_MaxButtonRetangle);
            }

            if(_CloseButtonHover == true)
            {
                _CloseButtonHover = false;
                this.Invalidate(_CloseButtonRectangle);
            }

            if(_HelpButtonHover == true)
            {
                _HelpButtonHover = false;
                this.Invalidate(_HelpButtonRectangle);
            }

            base.OnMouseLeave(e);
        }


重写windowstate:

public new FormWindowState WindowState
        {
            get { return this._WindowState; }
            set
            {
                if (!DesignMode)
                {
                    if (value == FormWindowState.Maximized)
                    {
                        this._NormalSize = this.Size;
                        this._NormalLocation = this.Location;

                        this.Location = new Point(0, 0);
                        this.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                        this.Invalidate();
                    }
                    else if (value == FormWindowState.Normal)
                    {
                        this.Size = _NormalSize;
                        this.Location = _NormalLocation;
                        this.Invalidate();
                    }
                    else if (value == FormWindowState.Minimized)
                    {
                        base.WindowState = value;
                    }
                }

                this._WindowState = value;
            }
        }


属性面板:



以及设计时可见的效果:





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值