
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using ryControls;

namespace ryControls
{
    /// <summary>
    /// 类说明：CContextMenuStrip控件的实现用来代替系统的ContextMenuStrip控件
    /// </summary>
    //可以使用一个图标来表示控件 ContextMenuStrip快捷菜单  
    [ToolboxBitmap(typeof(System.Windows.Forms.ContextMenuStrip))]
    public class RyContextMenuStrip : System.Windows.Forms.ContextMenuStrip
    {
        //窗口的边框长
        readonly int borderWidth = SystemInformation.BorderSize.Width;


        /// <summary>
        /// 
        /// </summary>
        public RyContextMenuStrip()
            : 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);

            //Office格式颜色
            ProfessionalColorTable ColorTable = new ProfessionalColorTable()
            {
                UseSystemColors = true
            };
            //是否为设计模式
            if (!DesignMode)
            {
                this.Renderer = new ryControls.ToolStripRenderer(ColorTable);
            }
            else
            {
                this.Renderer = new ToolStripProfessionalRenderer(ColorTable);
            }
            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public RyContextMenuStrip(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);

            ProfessionalColorTable ColorTable = new ProfessionalColorTable()
            {
                UseSystemColors = true
             };
            if (!DesignMode)
            {
                this.Renderer = new ryControls.ToolStripRenderer(ColorTable);
            }
            else
            {
                this.Renderer = new ToolStripProfessionalRenderer(ColorTable);
            }
            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);

        }

        /// <summary>
        /// 重写OnCreateControl
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            if (!DesignMode)
            {
                int Rgn = ryControls.Win32.CreateRoundRectRgn(1, 1, ClientSize.Width, Height, 7, 7);
                ryControls.Win32.SetWindowRgn(this.Handle, Rgn, true);
            }

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

        /// <summary>
        /// 重写OnPaint事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!DesignMode)
            {
                Graphics g = e.Graphics;

                SolidBrush bruch = new SolidBrush(Color.FromArgb(51, 166, 239));

                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(51, 132, 223), 1f);
                e.Graphics.DrawPath(pen, CreateRoundedRectanglePath(new Rectangle(1, 1, ClientSize.Width - 3, Height - 3), 5));
            }

            base.OnPaint(e);

        }

        /// <summary>
        /// CreateRoundedRectanglePath
        /// </summary>
        /// <param name="rect">Rectangle</param>
        /// <param name="cornerRadius"></param>
        /// <returns></returns>
        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;
        }
    }
}
