Esempio n. 1
0
 public static void CreateRegion(
     Control control,
     Rectangle bounds,
     int radius,
     RoundStyle roundStyle)
 {
     using (GraphicsPath path =
                GraphicsPathHelper.CreatePath(
                    bounds, radius, roundStyle, true))
     {
         Region region = new Region(path);
         path.Widen(Pens.White);
         region.Union(path);
         if (control.Region != null)
         {
             control.Region.Dispose();
         }
         control.Region = region;
     }
 }
        /// <summary>
        /// 渲染边框
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderToolStripBorder(
            ToolStripRenderEventArgs e)
        {
            ToolStrip toolStrip = e.ToolStrip;
            Graphics  g         = e.Graphics;
            Rectangle bounds    = e.AffectedBounds;

            if (toolStrip is ToolStripDropDown)
            {
                using (SmoothingModeGraphics sg = new SmoothingModeGraphics(g))
                {
                    using (GraphicsPath path =
                               GraphicsPathHelper.CreatePath(bounds, RoundRadius, RoundStyle.All, true))
                    {
                        using (Pen pen = new Pen(ColorTable.DropDownImageSeparator))
                        {
                            path.Widen(pen);
                            g.DrawPath(pen, path);
                        }
                    }
                }

                if (!(toolStrip is ToolStripOverflow))
                {
                    bounds.Inflate(-1, -1);
                    using (GraphicsPath innerPath = GraphicsPathHelper.CreatePath(
                               bounds, RoundRadius, RoundStyle.All, true))
                    {
                        using (Pen pen = new Pen(ColorTable.BackNormal))
                        {
                            g.DrawPath(pen, innerPath);
                        }
                    }
                }
            }
            else
            {
                base.OnRenderToolStripBorder(e);
            }
        }
        /// <summary>
        /// 渲染下拉左侧图标区域
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderImageMargin(
            ToolStripRenderEventArgs e)
        {
            ToolStrip toolStrip = e.ToolStrip;
            Graphics  g         = e.Graphics;
            Rectangle bounds    = e.AffectedBounds;

            if (toolStrip is ToolStripDropDown)
            {
                bool bDrawLogo    = NeedDrawLogo(toolStrip);
                bool bRightToLeft = toolStrip.RightToLeft == RightToLeft.Yes;

                Rectangle imageBackRect = bounds;
                //imageBackRect.Width = OffsetMargin;
                //画菜单右侧logo图片
                if (bDrawLogo)
                {
                    Rectangle logoRect = bounds;
                    //logoRect.Width = OffsetMargin;
                    //if (bRightToLeft)
                    //{
                    //    logoRect.X -= 2;
                    //    imageBackRect.X = logoRect.X - OffsetMargin;
                    //}
                    //else
                    //{
                    //    logoRect.X += 2;
                    //    imageBackRect.X = logoRect.Right;
                    //}
                    //logoRect.Y += 1;
                    //logoRect.Height -= 2;

                    using (LinearGradientBrush brush = new LinearGradientBrush(
                               logoRect,
                               ColorTable.BackHover,
                               ColorTable.BackNormal,
                               90f))
                    {
                        Blend blend = new Blend();
                        blend.Positions  = new float[] { 0f, .2f, 1f };
                        blend.Factors    = new float[] { 0f, 0.1f, .9f };
                        brush.Blend      = blend;
                        logoRect.Y      += 1;
                        logoRect.Height -= 2;
                        using (GraphicsPath path =
                                   GraphicsPathHelper.CreatePath(logoRect, RoundRadius, RoundStyle.All, false))
                        {
                            using (SmoothingModeGraphics sg = new SmoothingModeGraphics(g))
                            {
                                g.FillPath(brush, path);
                            }
                        }
                    }

                    StringFormat sf   = new StringFormat(StringFormatFlags.NoWrap);
                    Font         font = new Font(
                        toolStrip.Font.FontFamily, 11, FontStyle.Bold);
                    sf.Alignment     = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming      = StringTrimming.EllipsisCharacter;

                    g.TranslateTransform(logoRect.X, logoRect.Bottom);
                    g.RotateTransform(270f);

                    if (!string.IsNullOrEmpty(MenuLogoString))
                    {
                        Rectangle newRect = new Rectangle(
                            0, 0, logoRect.Height, logoRect.Width);

                        using (Brush brush = new SolidBrush(ColorTable.Fore))
                        {
                            using (TextRenderingHintGraphics tg =
                                       new TextRenderingHintGraphics(g))
                            {
                                g.DrawString(
                                    MenuLogoString,
                                    font,
                                    brush,
                                    newRect,
                                    sf);
                            }
                        }
                    }

                    g.ResetTransform();
                }
                else
                {
                    if (bRightToLeft)
                    {
                        imageBackRect.X -= 3;
                    }
                    else
                    {
                        imageBackRect.X += 3;
                    }
                }

                imageBackRect.Y      += 2;
                imageBackRect.Height -= 4;
                using (SolidBrush brush = new SolidBrush(ColorTable.DropDownImageBack))
                {
                    g.FillRectangle(brush, imageBackRect);
                }

                Point ponitStart;
                Point pointEnd;
                if (bRightToLeft)
                {
                    ponitStart = new Point(imageBackRect.X, imageBackRect.Y);
                    pointEnd   = new Point(imageBackRect.X, imageBackRect.Bottom);
                }
                else
                {
                    ponitStart = new Point(imageBackRect.Right - 1, imageBackRect.Y);
                    pointEnd   = new Point(imageBackRect.Right - 1, imageBackRect.Bottom);
                }

                using (Pen pen = new Pen(ColorTable.DropDownImageSeparator))
                {
                    g.DrawLine(pen, ponitStart, pointEnd);
                }
            }
            else
            {
                base.OnRenderImageMargin(e);
            }
        }
Esempio n. 4
0
        public static void RenderBackgroundInternal(
            Graphics g,
            Rectangle rect,
            Color baseColor,
            Color borderColor,
            Color innerBorderColor,
            RoundStyle style,
            int roundWidth,
            float basePosition,
            bool drawBorder,
            bool drawGlass,
            LinearGradientMode mode)
        {
            if (drawBorder)
            {
                rect.Width--;
                rect.Height--;
            }

            using (LinearGradientBrush brush = new LinearGradientBrush(
                       rect, Color.Transparent, Color.Transparent, mode))
            {
                Color[] colors = new Color[4];
                colors[0] = GetColor(baseColor, 0, 35, 24, 9);
                colors[1] = GetColor(baseColor, 0, 13, 8, 3);
                colors[2] = baseColor;
                colors[3] = GetColor(baseColor, 0, 35, 24, 9);

                ColorBlend blend = new ColorBlend();
                blend.Positions           = new float[] { 0.0f, basePosition, basePosition + 0.05f, 1.0f };
                blend.Colors              = colors;
                brush.InterpolationColors = blend;
                if (style != RoundStyle.None)
                {
                    using (GraphicsPath path =
                               GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                    {
                        g.FillPath(brush, path);
                    }

                    if (baseColor.A > 80)
                    {
                        Rectangle rectTop = rect;

                        if (mode == LinearGradientMode.Vertical)
                        {
                            rectTop.Height = (int)(rectTop.Height * basePosition);
                        }
                        else
                        {
                            rectTop.Width = (int)(rect.Width * basePosition);
                        }
                        using (GraphicsPath pathTop = GraphicsPathHelper.CreatePath(
                                   rectTop, roundWidth, RoundStyle.Top, false))
                        {
                            using (SolidBrush brushAlpha =
                                       new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                            {
                                g.FillPath(brushAlpha, pathTop);
                            }
                        }
                    }

                    if (drawGlass)
                    {
                        RectangleF glassRect = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            glassRect.Y      = rect.Y + rect.Height * basePosition;
                            glassRect.Height = (rect.Height - rect.Height * basePosition) * 2;
                        }
                        else
                        {
                            glassRect.X     = rect.X + rect.Width * basePosition;
                            glassRect.Width = (rect.Width - rect.Width * basePosition) * 2;
                        }
                        ControlPaintEx.DrawGlass(g, glassRect, 170, 0);
                    }

                    if (drawBorder)
                    {
                        using (GraphicsPath path =
                                   GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                        {
                            using (Pen pen = new Pen(borderColor))
                            {
                                g.DrawPath(pen, path);
                            }
                        }

                        rect.Inflate(-1, -1);
                        using (GraphicsPath path =
                                   GraphicsPathHelper.CreatePath(rect, roundWidth, style, false))
                        {
                            using (Pen pen = new Pen(innerBorderColor))
                            {
                                g.DrawPath(pen, path);
                            }
                        }
                    }
                }
                else
                {
                    g.FillRectangle(brush, rect);
                    if (baseColor.A > 80)
                    {
                        Rectangle rectTop = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            rectTop.Height = (int)(rectTop.Height * basePosition);
                        }
                        else
                        {
                            rectTop.Width = (int)(rect.Width * basePosition);
                        }
                        using (SolidBrush brushAlpha =
                                   new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                        {
                            g.FillRectangle(brushAlpha, rectTop);
                        }
                    }

                    if (drawGlass)
                    {
                        RectangleF glassRect = rect;
                        if (mode == LinearGradientMode.Vertical)
                        {
                            glassRect.Y      = rect.Y + rect.Height * basePosition;
                            glassRect.Height = (rect.Height - rect.Height * basePosition) * 2;
                        }
                        else
                        {
                            glassRect.X     = rect.X + rect.Width * basePosition;
                            glassRect.Width = (rect.Width - rect.Width * basePosition) * 2;
                        }
                        ControlPaintEx.DrawGlass(g, glassRect, 200, 0);
                    }

                    if (drawBorder)
                    {
                        using (Pen pen = new Pen(borderColor))
                        {
                            g.DrawRectangle(pen, rect);
                        }

                        rect.Inflate(-1, -1);
                        using (Pen pen = new Pen(innerBorderColor))
                        {
                            g.DrawRectangle(pen, rect);
                        }
                    }
                }
            }
        }