c#控件编写 (3)重绘From的非工作区

     重绘窗体的工作区的时候我们可以用到OnPaint来实现。而非工作区的绘制有几种思路

    1.直接实现WM_NCCALCSIZE消息绘制

    2.把窗体修改成None状态并拦截WM_NCCALCSIZE修改非工作区的大小实现

    3.把窗体修改成None直接做一个模拟的状态 

     这三种方法都会有一些弱点,这些弱点也许是我没有找到对应的解决方法 。

   1.在实现后需要修改 base.ControlBox = false,要不然在鼠标经过的时候会出现如下图的效果

image

其实我们是要的下面的这种效果

image

   但是这样做的后果是无法使用任务栏的菜单 ,如果有谁知道这个怎么解决的话留个言,在此谢过了 

 

  2.第二种实现的话在最大化最小化的时候,不知道是什么原因会产生窗体自动缩小在WM_NCCALCSIZE定义的标题栏的大小,这个也有一个折中的解决方法,就是重写SetBoundsCore然后注释掉里面的内容,但是在这样的话在Visual Studio里面就无法改变窗体的大小了。  如果这个也有解决方法的话,在此谢过了。

 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            //base.SetBoundsCore(x, y, width, height, specified);
        }

3.第三种当然是最好实现的了 ,但是模拟的状态无法直接替换到以前已经写好的窗体,直接否掉了 。

 

第一种和第二种其实是有点类似的,主要的地方还是在绘制上面  。

 

重绘非工作区,这个地方的重点在 DarwRegion函数里的

using (Bitmap img = new Bitmap(this.Bounds.Width, this.Bounds.Height))

{ Graphics g = Graphics.FromImage(img);

……

gs.DrawImage(img, Point.Empty);

}

这里利用双缓存先在图片上绘制了然后在画在窗体上

 

绘制函数 /// <summary>
        /// 重绘非工作区 
        /// </summary>
        private void NCPaint()
        {
            IntPtr wdc = Common.GetWindowDC(this.Handle);// Graphics.FromHwnd(this.Handle);

            Graphics g = Graphics.FromHdc(wdc);
            try
            {
                DarwRegion(g);
            }
            catch
            { }
            Common.ReleaseDC(this.Handle, wdc);
        }

        private void DarwRegion(Graphics gs)
        {
            if (this.FormBorderStyle == FormBorderStyle.None)
                return;

            using (Bitmap img = new Bitmap(this.Bounds.Width, this.Bounds.Height))
            {

                Graphics g = Graphics.FromImage(img);

                if (mouseMove == MouseMose.None)
                {
                    GraphicsPath drawRect = RectX(new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height), Arc);

                    //绘制渐变色 
                    Rectangle rect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
                    LinearGradientBrush brush = new LinearGradientBrush(rect, this._colorA, this._colorB, 90);
                    drawRect.AddRectangle(new Rectangle(BorderWidth, HeadHeight, this.ClientSize.Width, this.ClientSize.Height));
                    g.FillPath(brush, drawRect);


                    //绘制图标Icon
                    if (ShowIcon)
                        g.DrawIcon(this.Icon, new Rectangle(BorderWidth + 5, (HeadHeight - 16) / 2, 16, 16));

                    //绘制标题 

                    StringFormat sf = new StringFormat();
                    sf.LineAlignment = StringAlignment.Center;
                    //求标题栏的区域
                    ToolTitle = new Rectangle(BorderWidth + 23, 0, this.Bounds.Width, HeadHeight);
                    g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), ToolTitle, sf);

                    //绘制最大最小关闭按钮  
                    //g.DrawImage(butClose, ButClose);
                    //g.DrawImage(butMax, ButMax);
                    //g.DrawImage(butMin, ButMin);

                    DrawButton(g);

                    //绘制边框  
                    g.SmoothingMode = SmoothingMode.HighQuality; //高质量
                    drawRect = RectX(new Rectangle(1, 1, this.Bounds.Width - 4, this.Bounds.Height - 4), Arc - 1);
                    g.DrawPath(new Pen(Color.FromArgb(100, Color.White), 1), drawRect);//绘制内边框

                    drawRect = RectX(new Rectangle(0, 0, this.Bounds.Width - 2, this.Bounds.Height - 2), Arc - 1);
                    g.DrawPath(new Pen(SolidColor, 1), drawRect);//绘制外边框 
                }
                else
                {
                    int i = this.Bounds.Width - 3, h = 1;
                    foreach (ToolButton a in ListButton)
                    {
                        if (!a.Start)
                            continue;
                        i = i - a.Size.Width;
                        Rectangle rect = new Rectangle(new Point(i, h), a.Size);
                        if (a == _newbutton)
                        {
                            g.FillRectangle(new SolidBrush(ColorA), rect);
                            g.DrawImage(a.Move, rect);
                        }
                        else
                            if (a == _oldbutton)
                            {
                                g.FillRectangle(new SolidBrush(ColorA), rect);
                                g.DrawImage(a.Default, rect);
                            }
                    }

                }
                gs.DrawImage(img, Point.Empty);
                
            }

          

        }


 
三个按钮的绘制,可以参考我前一篇重写TabControl标签的方法  。

最后是几个需要重绘的消息 

非客户区刷新 /// <summary>
        /// 非客户区刷新 
        /// </summary>
        private void NCUpdate()
        {
            Common.SendMessage(this.Handle, (int)Msg.WM_NCPAINT, 0, 0);
        }

非客户区刷新 switch (m.Msg)
            {

                case (int)Msg.WM_NCCALCSIZE:
                    BorderWidth = (this.Bounds.Width - this.ClientSize.Width) / 2;
                    HeadHeight = this.Bounds.Height - this.ClientSize.Height - BorderWidth;
                    mouseMove = MouseMose.None;
                    // NCPaint();
                    base.WndProc(ref m);
                    break;
                case (int)Msg.WM_NCACTIVATE:
                    base.WndProc(ref m);
                    mouseMove = MouseMose.None;
                    NCUpdate();
                    break;
                case (int)Msg.WM_NCPAINT:
                    NCPaint();
                    break;
                case (int)Msg.WM_NCLBUTTONDOWN:
                    if (GetMouseButton() == null)
                    {
                        base.WndProc(ref m);
                        // Console.WriteLine(1);
                    }
                    break;
                case (int)Msg.WM_NCLBUTTONUP:
                    ToolButton but = GetMouseButton();
                    if (but != null)
                    {
                        but.OnClick();
                    }
                    base.WndProc(ref m);
                    break;
                case (int)Msg.WM_NCMOUSEMOVE:
                    NewButton = GetMouseButton();
                    base.WndProc(ref m);
                    break;
                case (int)Msg.WM_ERASEBKGND:
                    base.WndProc(ref m);
                    NCPaint();
                    mouseMove = MouseMose.None;
                    NCUpdate();
                  
                    break;

                
                default:
                    base.WndProc(ref m);
                    break;
            }

在这里鼠标点击关闭按钮是会出现WM_NCLBUTTONDOWN无法接收到消息,研究了很久发现了一个可以收到的方法,在WM_NCLBUTTONUP事件中判断是否是在最大最小关闭按钮按下的,如果是就不执行 base.WndProc( ref m)这句。

        基本上窗体绘制的原理就这么些东西了,如果哪位同学对上面的方法有更好的建议的话,欢迎讨论 。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值