珂珂的个人博客 - 一个程序猿的个人网站

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十)-右键菜单

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(一)-效果图

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(二)-基窗体

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(三)-主窗体

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(四)-MessageBox

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(五)-按钮1

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(六)-按钮2

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(七)-按钮3

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(八)-文本框

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(九)-有边框的PictureBox

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十)-右键菜单

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十一)-换颜色

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十二)-换背景

FYJ.Winform皮肤-窗体组件-仿QQ调色面板(十三)-TabControl

右键菜单的绘制有点特别  它是通过Renderer 属性,所以绘制部分是继承ToolStripProfessionalRenderer

右键菜单的代码继承自ContextMenuStrip

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using FYJ.Winform.Util;

namespace FYJ.Winform.Controls
{
    public class ContextMenuStripEx : ContextMenuStrip
    {

         int borderWidth = SystemInformation.BorderSize.Width;

        public ContextMenuStripEx()
            : base()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            ToolStripProfessionalRendererEx render = new ToolStripProfessionalRendererEx();

            this.Renderer = render;

            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);
         
        }

        public ContextMenuStripEx(IContainer container)
            : base(container)
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            ToolStripProfessionalRendererEx render = new ToolStripProfessionalRendererEx();

            this.Renderer = render;
            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);

        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            //if (!DesignMode)
            //{
            //    //GraphicsPath tpath = new GraphicsPath(FillMode.Winding);
            //    //tpath.AddRectangle(new Rectangle(borderWidth, borderWidth, Width - 2 * borderWidth - 5, Height + 1));

            //    //this.Region = new Region(tpath);

            //    //int Rgn;
            //    //Rgn = Win32.CreateRoundRectRgn(0, 0, Width + borderWidth, Height + 1,7, 7);
            //    //Win32.SetWindowRgn(this.Handle, Rgn, false);

            //    //this.Region = new Region(new Rectangle(3, 3, this.ClientRectangle.Width - 2 * 3, this.ClientRectangle.Height - 2 * 3));

            //    int Rgn = Win32.CreateRoundRectRgn(1, 1, ClientSize.Width , Height , 7, 7);
            //    Win32.SetWindowRgn(this.Handle, Rgn, true);
            //}

            //int result = Win32.SetClassLong(this.Handle, Win32.GCL_STYLE, 0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //if (!DesignMode)
            //{
            //    Graphics g = e.Graphics;

            //    SolidBrush bruch = new SolidBrush(FYJ.Winform.Util.MyFormTemp.CurrentBackgroundColor);
                
            //    g.FillRectangle(bruch, 0, 0, 25, this.Height);

            //    bruch = new SolidBrush(Color.White);
            //    g.FillRectangle(bruch, 27, 0, this.Width - 27, this.Height);

            //   // Pen pen = new Pen(Color.FromArgb(25, 85, 95), 1f);
            //    Pen pen = new Pen(FYJ.Winform.Util.MyFormTemp.CurrentBackgroundColor2, 1f);
            //    e.Graphics.DrawPath(pen, CreateRoundedRectanglePath(new Rectangle(1, 1, ClientSize.Width-3, Height-3), 5));
            //}

            base.OnPaint(e);
            //画内部
            //Graphics g = e.Graphics;

            //LinearGradientBrush bruch = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.FromArgb(200, 255, 200), LinearGradientMode.BackwardDiagonal);
            //g.FillRectangle(bruch, this.ClientRectangle);
            ////画边框
            //Pen pen = new Pen(Color.Red, 3);
            //g.DrawRectangle(pen, this.ClientRectangle);
        }
       
        //internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
        //{
        //    GraphicsPath roundedRect = new GraphicsPath();
        //    roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
        //    roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
        //    roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
        //    roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
        //    roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
        //    roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
        //    roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
        //    roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
        //    roundedRect.CloseFigure();
        //    return roundedRect;
        //}

        protected override void OnOpening(CancelEventArgs e)
        {
            base.OnOpening(e);
            ((ToolStripProfessionalRendererEx)(this.Renderer)).CurrentColor = FYJ.Winform.Util.MyFormTemp.CurrentBackgroundColor;
        }
    }
}


绘制类ToolStripProfessionalRendererEx的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using FYJ.Winform.Util;

namespace FYJ.Winform.Controls
{
   public class ToolStripProfessionalRendererEx:ToolStripProfessionalRenderer
    {
       private Color _color = MyFormTemp.CurrentBackgroundColor;
       public Color CurrentColor
       {
           get { return _color; }
           set { _color = value; }
       }
         public ToolStripProfessionalRendererEx():base()
         {
        }
         public ToolStripProfessionalRendererEx(Color color)
             : base()
        {
            _color = color;
         }
       
         //渲染背景 包括menustrip背景 toolstripDropDown背景
         protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
         {
             
             base.OnRenderToolStripBackground(e);
             ToolStrip toolStrip = e.ToolStrip;
             Graphics g = e.Graphics;
             g.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
             Rectangle bounds = e.AffectedBounds;
             LinearGradientBrush lgbrush = new LinearGradientBrush(new Point(0, 0), new Point(0, toolStrip.Height), Color.FromArgb(255, Color.White), Color.FromArgb(150, _color));
         
              if (toolStrip is ToolStripDropDown)
             {
                 g.FillRectangle(lgbrush, new Rectangle(25, 0, toolStrip.Size.Width-25, toolStrip.Size.Height));
             }
             else
             {
                 base.OnRenderToolStripBackground(e);
             }
         }
         //渲染边框 
         protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
         {
             Pen pen = new Pen(_color, 1);
             e.Graphics.DrawRectangle(pen, e.AffectedBounds);
    
                // int Rgn = Win32.CreateRoundRectRgn(1, 1, e.ToolStrip.Width , e.ToolStrip.Height , 3, 3);
               // Win32.SetWindowRgn(e.ToolStrip.Handle, Rgn, true);
             //base.OnRenderToolStripBorder(e);
         }

      
         //渲染箭头 更改箭头颜色
         protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
         {
             e.ArrowColor = _color;
             base.OnRenderArrow(e);
         }
         //渲染项 不调用基类同名方法
         protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
         {
             Graphics g = e.Graphics;
             g.SmoothingMode = SmoothingMode.HighQuality;
             ToolStripItem item = e.Item;
             ToolStrip toolstrip = e.ToolStrip;

             //渲染下拉项
              if (toolstrip is ToolStripDropDown)
             {
                 if (item.Selected)
                 {
                     SolidBrush brush = new SolidBrush(_color);
                     g.FillRectangle(brush, new Rectangle(0, 0, item.Width, item.Height));
                 }
             }
             else
             {
                 base.OnRenderMenuItemBackground(e);
             }
         }

       //渲染分隔线
         protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
         {
             Graphics g = e.Graphics;
             Pen pen = new Pen(_color, 1);
             g.DrawLine(pen,new Point(25,0),new Point(e.Item.Width,0));
         }
         //渲染图片区域 下拉菜单左边的图片区域
         protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
         {
             base.OnRenderImageMargin(e);
             //屏蔽掉左边图片竖条
         }

       
    }
}



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧