Пример #1
0
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Graphics g = pevent.Graphics;

            ButtonRenderer.DrawParentBackground(g, ClientRectangle, this);
            float Angle = 90F;

            if (!isHotTracking)
            {
                paintGradientTop         = gradientTop;
                paintGradientBottom      = gradientBottom;
                paintGradientBorderColor = gradientBorderColor;
                paintForeColor           = ForeColor;
            }
            else
            {
                paintGradientTop         = _hotGradientTop;
                paintGradientBottom      = _hotGradientBottom;
                paintGradientBorderColor = _hotGradientBorderColor;
                paintForeColor           = _hotForeColor;
            }
            if (isPressed)
            {
                paintGradientTop         = _pressedGradientTop;
                paintGradientBottom      = _pressedGradientBottom;
                paintGradientBorderColor = _pressedGradientBorderColor;
                paintForeColor           = _pressedForeColor;
            }


            // Paint the outer rounded rectangle
            g.SmoothingMode = SmoothingMode.AntiAlias;
            using (GraphicsPath outerPath = RoundedRectangle(buttonRect, rectCornerRadius, 0))
            {
                using (LinearGradientBrush outerBrush = new LinearGradientBrush(buttonRect, Color.White, Color.White, LinearGradientMode.Vertical))
                {
                    g.FillPath(outerBrush, outerPath);
                }
                using (Pen outlinePen = new Pen(paintGradientBorderColor, rectOutlineWidth))
                {
                    outlinePen.Alignment = PenAlignment.Inset;
                    g.DrawPath(outlinePen, outerPath);
                }
            }
            // If this is the default button, paint an additional highlight
            if (IsDefault)
            {
                using (GraphicsPath defaultPath = new GraphicsPath())
                {
                    defaultPath.AddPath(RoundedRectangle(buttonRect, rectCornerRadius, 0), false);
                    defaultPath.AddPath(RoundedRectangle(buttonRect, rectCornerRadius, defaultHighlightOffset), false);
                    using (PathGradientBrush defaultBrush = new PathGradientBrush(defaultPath))
                    {
                        defaultBrush.CenterColor    = Color.FromArgb(50, Color.White);
                        defaultBrush.SurroundColors = new Color[] { Color.FromArgb(150, Color.White) };
                        g.FillPath(defaultBrush, defaultPath);
                    }
                }
            }
            // Paint the gel highlight
            using (GraphicsPath innerPath = RoundedRectangle(highlightRect, rectCornerRadius, highlightRectOffset))
            {
                //using (LinearGradientBrush innerBrush = new LinearGradientBrush(highlightRect, Color.FromArgb(highlightAlphaTop, Color.White), Color.FromArgb(highlightAlphaBottom, Color.White), LinearGradientMode.Vertical))
                //{
                g.FillPath(DrawingMethods.GetBrush(ClientRectangle, paintGradientTop, paintGradientBottom, DrawingMethods.PaletteColorStyle.Default, Angle, DrawingMethods.VisualOrientation.Top, false), innerPath);
                //g.FillPath(innerBrush, innerPath);
                //}
            }
            // Paint the text
            //TextRenderer.DrawText(g, Text, Font, buttonRect, paintForeColor, Color.Transparent, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
            OnDrawTextAndImage(g);
        }
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, bool up)
        {
            PushButtonState pbState = DetermineState(up);

            // First handle transparent case

            if (ButtonRenderer.IsBackgroundPartiallyTransparent(pbState))
            {
                ButtonRenderer.DrawParentBackground(e.Graphics, bounds, Control);
            }

            // Now draw the actual themed background
            if (!DpiHelper.IsScalingRequirementMet)
            {
                ButtonRenderer.DrawButton(e.Graphics, Control.ClientRectangle, false, pbState);
            }
            else
            {
                ButtonRenderer.DrawButtonForHandle(e.Graphics, Control.ClientRectangle, false, pbState, Control.HandleInternal);
            }

            // Now overlay the background image or backcolor (the former overrides the latter), leaving a
            // margin. We hardcode this margin for now since GetThemeMargins returns 0 all the
            // time.
            // Changing this because GetThemeMargins simply does not
            // work in some cases.
            bounds.Inflate(-buttonBorderSize, -buttonBorderSize);

            //only paint if the user said not to use the themed backcolor.
            if (!Control.UseVisualStyleBackColor)
            {
                bool  painted = false;
                bool  isHighContrastHighlighted = up && IsHighContrastHighlighted();
                Color color = isHighContrastHighlighted ? SystemColors.Highlight : Control.BackColor;

                // Note: PaintEvent.HDC == 0 if GDI+ has used the HDC -- it wouldn't be safe for us
                // to use it without enough bookkeeping to negate any performance gain of using GDI.
                if (color.A == 255 && e.HDC != IntPtr.Zero)
                {
                    if (DisplayInformation.BitsPerPixel > 8)
                    {
                        var r = new RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
                        // SysColorBrush does not have to be deleted.
                        User32.FillRect(
                            new HandleRef(e, e.HDC),
                            ref r,
                            new HandleRef(this, isHighContrastHighlighted ? User32.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF) : Control.BackColorBrush));
                        painted = true;
                    }
                }

                if (!painted)
                {
                    // don't paint anything from 100% transparent background
                    //
                    if (color.A > 0)
                    {
                        if (color.A == 255)
                        {
                            color = e.Graphics.GetNearestColor(color);
                        }

                        // Color has some transparency or we have no HDC, so we must
                        // fall back to using GDI+.
                        //
                        using (Brush brush = new SolidBrush(color))
                        {
                            e.Graphics.FillRectangle(brush, bounds);
                        }
                    }
                }
            }

            //This code is mostly taken from the non-themed rendering code path.
            if (Control.BackgroundImage != null && !DisplayInformation.HighContrast)
            {
                ControlPaint.DrawBackgroundImage(e.Graphics, Control.BackgroundImage, Color.Transparent, Control.BackgroundImageLayout, Control.ClientRectangle, bounds, Control.DisplayRectangle.Location, Control.RightToLeft);
            }
        }
        /// <summary>
        /// Primary function for painting the button. This method should be overridden instead of OnPaint.
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="bounds">The bounds.</param>
        protected virtual void PaintButton(Graphics graphics, Rectangle bounds)
        {
            System.Diagnostics.Debug.WriteLine($"PaintButton: desMode:{this.IsDesignMode()};vsEnabled:{Application.RenderWithVisualStyles};vsOnOS:{VisualStyleInformation.IsSupportedByOS};btnState:{ButtonState};enabled:{Enabled};imgCt:{(ImageList != null ? ImageList.Images.Count : 0)}");

            if (InitializeRenderer())
            {
                if (OnGlass)
                {
                    rnd.DrawGlassBackground(graphics, bounds, bounds);
                }
                else
                {
                    rnd.DrawParentBackground(graphics, bounds, this);
                    rnd.DrawBackground(graphics, bounds);
                }
            }
            else
            {
                if (ImageList != null && ImageList.Images.Count > 0)
                {
                    int idx = (int)ButtonState - 1;
                    if (ImageList.Images.Count == 1)
                    {
                        idx = 0;
                    }
                    else if (ImageList.Images.Count == 2)
                    {
                        idx = ButtonState == PushButtonState.Disabled ? 1 : 0;
                    }
                    else if (ImageList.Images.Count == 3)
                    {
                        idx = ButtonState == PushButtonState.Normal ? 0 : idx - 1;
                    }
                    bool forceDisabled = !Enabled && ImageList.Images.Count == 1;
                    if (OnGlass)
                    {
                        VisualStyleRendererExtension.DrawGlassImage(null, graphics, bounds, ImageList.Images[idx], forceDisabled);
                    }
                    else
                    {
                        if (!Application.RenderWithVisualStyles && VisualStyleInformation.IsSupportedByOS)
                        {
                            System.Drawing.Drawing2D.GraphicsContainer g = graphics.BeginContainer();
                            Rectangle translateRect = bounds;
                            graphics.TranslateTransform(-bounds.Left, -bounds.Top);
                            PaintEventArgs pe = new PaintEventArgs(graphics, translateRect);
                            InvokePaintBackground(Parent, pe);
                            InvokePaint(Parent, pe);
                            graphics.ResetTransform();
                            graphics.EndContainer(g);
                        }
                        else
                        {
                            graphics.Clear(Parent.BackColor);
                        }
                        if (forceDisabled)
                        {
                            ControlPaint.DrawImageDisabled(graphics, ImageList.Images[idx], 0, 0, Color.Transparent);
                        }
                        else
                        {
                            //base.ImageList.Draw(graphics, bounds.X, bounds.Y, bounds.Width, bounds.Height, idx);
                            //VisualStyleRendererExtender.DrawGlassImage(null, graphics, bounds, base.ImageList.Images[idx], forceDisabled); // Not 7
                            graphics.DrawImage(ImageList.Images[idx], bounds, bounds, GraphicsUnit.Pixel); // Works on XP, not 7, with Parent.BackColor
                        }
                    }
                }

                /*else if (this.ImageList != null && this.ImageList.Images.Count > 1)
                 *              {
                 *                      int idx = (int)ButtonState - 1;
                 *                      if (this.ImageList.Images.Count == 2)
                 *                              idx = ButtonState == PushButtonState.Disabled ? 1 : 0;
                 *                      if (this.ImageList.Images.Count == 3)
                 *                              idx = ButtonState == PushButtonState.Normal ? 0 : idx - 1;
                 *                      if (rnd != null && !this.IsDesignMode() && DesktopWindowManager.IsCompositionEnabled())
                 *                              rnd.DrawGlassIcon(graphics, bounds, this.ImageList, idx);
                 *                      else
                 *                              this.ImageList.Draw(graphics, bounds.X, bounds.Y, bounds.Width, bounds.Height, idx);
                 *              }*/
                // No image so draw standard button
                else
                {
                    ButtonRenderer.DrawParentBackground(graphics, bounds, this);
                    ButtonRenderer.DrawButton(graphics, bounds, ButtonState);
                }
            }

            if (Focused)
            {
                ControlPaint.DrawFocusRectangle(graphics, bounds);
            }
        }
Пример #4
0
        protected override void OnPaint(PaintEventArgs e)
        {
            ButtonRenderer.DrawParentBackground(e.Graphics, e.ClipRectangle, this);
            //this.PaintTransparentBackground(e.Graphics, e.ClipRectangle);

            //now let's we begin painting
            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            if (!this.Enabled)
            {
                tBorder           = MetroPaint.BorderColor.Button.Disabled(Theme);
                tBottomColorBegin = MetroPaint.BackColor.Button.Disabled(Theme);
                tBottomColorEnd   = MetroPaint.BackColor.Button.Disabled(Theme);
                Textcol           = MetroPaint.ForeColor.Button.Disabled(Theme);
            }
            else
            {
                //Normal State
                tBorder           = highlight ? MetroPaint.GetStyleColor(Style) : MetroPaint.BorderColor.Button.Normal(Theme);
                tBottomColorBegin = MetroPaint.BackColor.Button.Normal(Theme);
                tBottomColorEnd   = !flatMetroAppearance?MetroPaint.BackColor.Button.Normal2(Theme) : MetroPaint.BackColor.Button.Normal(Theme);

                Textcol = MetroPaint.ForeColor.Button.Normal(Theme);
                //hot tracking - mouse over
                if (isHotTracking)
                {
                    tBorder           = MetroPaint.BorderColor.Button.Hover(Theme);
                    tBottomColorBegin = MetroPaint.BackColor.Button.Hover(Theme);
                    tBottomColorEnd   = !flatMetroAppearance?MetroPaint.BackColor.Button.Hover2(Theme) : MetroPaint.BackColor.Button.Hover(Theme);

                    Textcol = MetroPaint.ForeColor.Button.Hover(Theme);
                }
                //pressed
                if (isPressed)
                {
                    tBorder           = MetroPaint.BorderColor.Button.Press(Theme);
                    tBottomColorBegin = MetroPaint.BackColor.Button.Press(Theme);
                    tBottomColorEnd   = !flatMetroAppearance?MetroPaint.BackColor.Button.Press2(Theme) : MetroPaint.BackColor.Button.Press(Theme);

                    Textcol = MetroPaint.ForeColor.Button.Press(Theme);
                }
                //focused but not pressed
                if ((this.Focused) && (!isPressed))
                {
                    tBorder           = highlight ? MetroPaint.GetStyleColor(Style) : MetroPaint.BorderColor.Button.Normal(Theme);
                    tBottomColorBegin = MetroPaint.BackColor.Button.Normal(Theme);
                    tBottomColorEnd   = !flatMetroAppearance?MetroPaint.BackColor.Button.Normal2(Theme) : MetroPaint.BackColor.Button.Normal(Theme);

                    Textcol = MetroPaint.ForeColor.Button.Normal(Theme);
                }
                //focused and mouse over
                if ((this.Focused) && (isHotTracking))
                {
                    tBorder           = MetroPaint.BorderColor.Button.Hover(Theme);
                    tBottomColorBegin = MetroPaint.BackColor.Button.Hover(Theme);
                    tBottomColorEnd   = !flatMetroAppearance?MetroPaint.BackColor.Button.Hover2(Theme) : MetroPaint.BackColor.Button.Hover(Theme);

                    Textcol = MetroPaint.ForeColor.Button.Hover(Theme);
                }
            }

            //ClearType
            try
            {
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            //paint background
            PaintBackground(e, g, tBorder, tBottomColorBegin, tBottomColorEnd);

            //paint Text
            DrawTextAndImage(g, Textcol);
        }
Пример #5
0
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;

            graphics.Clear(Parent.BackColor);
            graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            GraphicsPath controlGraphicsPath = new GraphicsPath();

            switch (_pickType)
            {
            case PickerType.Rectangle:
            {
                _border.HoverVisible = false;

                _graphicsBuffer.FillRectangle(_spectrumGradient, ClientRectangle);
                _graphicsBuffer.FillRectangle(_blackBottomGradient, 0, (Height * 0.7f) + 1, Width, Height * 0.3f);
                _graphicsBuffer.FillRectangle(_whiteTopGradient, 0, 0, Width, Height * 0.3f);
                e.Graphics.DrawImageUnscaled(_canvas, Point.Empty);

                controlGraphicsPath = new GraphicsPath();
                controlGraphicsPath.AddRectangle(new RectangleF(ClientRectangle.Location, new Size(Width - 1, Height - 1)));
                break;
            }

            case PickerType.Wheel:
            {
                OnPaintBackground(e);

                // If the parent is using a transparent colorManager, it's likely to be something like a TabPage in a tab control
                // so we'll draw the parent background instead, to avoid having an ugly solid colorManager
                if ((BackgroundImage == null) && (Parent != null) && ((BackColor == Parent.BackColor) || (Parent.BackColor.A != 255)))
                {
                    ButtonRenderer.DrawParentBackground(e.Graphics, DisplayRectangle, this);
                }

                if (_brush != null)
                {
                    e.Graphics.FillPie(_brush, ClientRectangle, 0, 360);
                }

                // Smooth out the edge of the wheel.
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                using (Pen pen = new Pen(BackColor, 2))
                {
                    e.Graphics.DrawEllipse(pen, new RectangleF(_centerPoint.X - _radius, _centerPoint.Y - _radius, _radius * 2, _radius * 2));
                }

                Point pointLocation = new Point(Convert.ToInt32(_centerPoint.X - _radius), Convert.ToInt32(_centerPoint.Y - _radius));
                Size  newSize       = new Size(Convert.ToInt32(_radius * 2), Convert.ToInt32(_radius * 2));

                controlGraphicsPath = new GraphicsPath();
                controlGraphicsPath.AddEllipse(new RectangleF(pointLocation, newSize));

                break;
            }
            }

            VisualBorderRenderer.DrawBorderStyle(graphics, _border, controlGraphicsPath, MouseState);

            // Draws the button
            if (!Color.IsEmpty && _pickerVisible)
            {
                DrawColorPicker(e, HslManager, _drawFocusRectangle);
            }
        }
Пример #6
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (Size.Width == 0 || Size.Height == 0)
            {
                return;
            }

            Rectangle bounds = new Rectangle(Point.Empty, Size);

            if (!Application.RenderWithVisualStyles)
            {
                e.Graphics.FillRectangle(NonVisualStylesBrush, bounds);
            }
            else
            {
                using (Brush b = GradientBrush(bounds))
                {
                    ButtonRenderer.DrawParentBackground(e.Graphics, bounds, this);
                    e.Graphics.FillRectangle(b, bounds);
#if xmas
                    if (StarsEnabled)
                    {
                        // Update position of each star
                        foreach (Star star in stars)
                        {
                            if (star.isEgg)
                            {
                                star.Y += MaxSpeed * 5;
                            }
                            else
                            {
                                star.Y     += star.Radius * MaxSpeed;
                                star.Theta += star.AngularVelocity;
                            }
                        }

                        // Remove stars that have fallen off the bottom
                        stars.RemoveAll(delegate(Star star)
                        {
                            return(star.Y - star.Radius > this.Bottom);
                        });

                        if (stars.Count < MaxStars && random.NextDouble() < NewStarProbability)
                        {
                            // Add new star
                            Star newStar = new Star();

                            newStar.Radius = random.Next(MinStarSize, MaxStarSize);
                            newStar.X      = random.Next(0, bounds.Width);
                            newStar.Y      = -newStar.Radius;

                            newStar.AngularVelocity = (float)(random.NextDouble() * MaxAngularVelocity);
                            if (random.NextDouble() > 0.5)
                            {
                                newStar.AngularVelocity = -newStar.AngularVelocity;
                            }

                            if (random.NextDouble() < EggProbability)
                            {
                                newStar.isEgg  = true;
                                newStar.Radius = 40;
                                newStar.Y      = -newStar.Radius;
                                newStar.Theta  = 0.1f * (float)(-0.5 + random.NextDouble());
                            }
                            else
                            {
                                newStar.NumPoints = random.Next(MinStarPoints, MaxStarPoints);
                                newStar.Spikiness = random.Next(MinSpikiness, MaxSpikiness) / 10.0f;
                            }

                            if (!ContainsClose(stars, newStar, MinStarSeparation))
                            {
                                // Reject star if there is another nearby
                                stars.Add(newStar);
                            }
                        }

                        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

                        foreach (Star star in stars)
                        {
                            e.Graphics.ResetTransform();
                            e.Graphics.TranslateTransform(star.X, star.Y);
                            e.Graphics.RotateTransform(360 * star.Theta);
                            if (star.isEgg)
                            {
                                e.Graphics.ScaleTransform(0.35f, 0.35f);

                                using (Pen bluePen = new Pen(Color.SteelBlue, 3f), redPen = new Pen(Color.Red, 3f))
                                {
                                    e.Graphics.FillPath(Brushes.MistyRose, EggBeziers);
                                    e.Graphics.DrawPath(redPen, EggStripe);
                                    e.Graphics.FillPath(Brushes.Yellow, EggStripe);
                                    e.Graphics.DrawPath(bluePen, EggBeziers);
                                }
                            }
                            else
                            {
                                PointF[] points = GetStarShape(star.NumPoints, star.Radius, star.Spikiness);
                                e.Graphics.DrawPolygon(Pens.DodgerBlue, points);
                                using (SolidBrush brush = new SolidBrush(Color.FromArgb(200, Color.LightBlue)))
                                {
                                    e.Graphics.FillPolygon(brush, points);
                                }
                            }
                        }
                    }
#endif
                }
            }

            if (scheme == Schemes.Title)
            {
                using (Pen p = new Pen(Program.TitleBarBorderColor))
                {
                    e.Graphics.DrawRectangle(p, new Rectangle(bounds.Left, bounds.Top, bounds.Width - 1, bounds.Height - 1));
                }
            }
        }
Пример #7
0
 /// <summary>
 /// 자식 컨트롤의 부모를 배경으로 그린다. (투명 효과 낼때.. 배경이 보여야 할때..)
 /// </summary>
 /// <param name="g">배경을 그릴 때 사용되는 System.Drawing.Graphics입니다.</param>
 /// <param name="bounds">배경의 범위를 지정하는 System.Drawing.Rectangle입니다.</param>
 /// <param name="childControl">자식 컨트롤 입니다.</param>
 public static void DrawParentBackground(Graphics g, Rectangle bounds, Control childControl)
 {
     ButtonRenderer.DrawParentBackground(g, bounds, childControl);
 }