• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

WinformC#标题栏自定义样式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

#region 改变窗口样式

        #if !DEBUG

        [DllImport ("User32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);

        [DllImport ("User32.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);


        protected override void WndProc(ref Message m)

        ...{
   
                base.WndProc(ref m);
        
                switch(m.Msg)

       
                ...{

                    case 0x86://WM_NCACTIVATE

                        goto case 0x85;

                    case 0x85://WM_NCPAINT
                    ...{
                       
         
                        IntPtr hDC = GetWindowDC(m.HWnd);
    
                        //把DC转换为.NET的Graphics就可以很方便地使用Framework提供的绘图功能了
                        Graphics gs = Graphics.FromHdc(hDC);

                        处理色彩#region 处理色彩

                        int ibox = 1;

                        if (this.MaximizeBox) ibox ++;
                        if (this.MinimizeBox) ibox ++;

                        //得到相关背景图片
                        Image imgpm = Image.FromFile( Application.StartupPath+ @"Main.bmp");

                    
                       
                      //gs.DrawImage(imgbg,3,0,this.Width - (ibox * iBoxWidh),SystemInformation.CaptionHeight + 2);    //显示背景图片

                        //标题栏显示背景颜色
                        int xPos    = this.Width    -    iBoxWidh * ibox - 2 - ibox*3;

                        Rectangle excludeRect;
                        //标题栏中不需要填充的位置
                        for(int i=0;i<ibox;i++)
                        ...{
                            if( i>0)
                            ...{
                                xPos = xPos + iBoxWidh + 2;
                            }
                            
                              excludeRect = new Rectangle(xPos, 5, iBoxWidh, iBoxWidh+1);
                            gs.ExcludeClip(excludeRect);
                        }
                       
                   
                        //背景区域
                        Rectangle rBackground = new Rectangle(0,0,this.Width,SystemInformation.CaptionHeight+3);
                        //背景颜色 上下渐变
                        System.Drawing.Drawing2D.LinearGradientBrush bBackground
                            = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground, Color.FromArgb(100,145,175), Color.FromArgb(45,80,120),LinearGradientMode.Vertical);

                        //标题栏填充背景颜色
                        gs.FillRectangle(bBackground,rBackground );
                        //显示图标文件
                      gs.DrawImage(imgpm,4,4,24,24);   

                        #endregion


                        StringFormat strFmt = new StringFormat();

                        //strFmt.Alignment = StringAlignment.Center;
                        //strFmt.LineAlignment = StringAlignment.Center;

                        //gs.DrawString(this.Text, this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);
                        //设置标题字体
                        Font drawFont = new Font("宋体", 10,System.Drawing.FontStyle.Bold);
                        //设置标题颜色
                        SolidBrush drawBrush = new SolidBrush(Color.White);
                    
                        //重画标题
                        gs.DrawString(this.Text, drawFont, drawBrush, 30,8);

                        gs.Dispose();

                        //释放GDI资源

                        ReleaseDC(m.HWnd, hDC);
                        
                        break;

                    }

                    case 0xA1://WM_NCLBUTTONDOWN
                    ...{

                        Point mousePoint = new Point((int)m.LParam);

                        mousePoint.Offset(-this.Left, -this.Top);

                        //                    if(m_rect.Contains(mousePoint))
                        //                    {
                        //
                        //                        MessageBox.Show("hello");
                        //
                        //                    }

                        break;

                    }
    
                }

            }
        
        #endif

        #endregion


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#读写ini文件发布时间:2022-07-18
下一篇:
c#任意角度旋转图片发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap