Beispiel #1
0
        /// <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 override void PaintButton(Graphics graphics, Rectangle bounds)
        {
            if (Application.RenderWithVisualStyles)
            {
                try
                {
                    VisualStyleRenderer rnd = new VisualStyleRenderer(StyleClass, StylePart, (int)ButtonState);
                    if (this.IsDesignMode() || !DesktopWindowManager.IsCompositionEnabled())
                    {
                        rnd.DrawParentBackground(graphics, bounds, this);
                        rnd.DrawBackground(graphics, bounds);
                    }
                    else
                    {
                        rnd.DrawGlassBackground(graphics, bounds, bounds);
                    }
                    return;
                }
                catch { }
            }
            else
            {
                base.PaintButton(graphics, bounds);

                /*Rectangle sr = this.ClientRectangle;
                 * sr.Offset(0, sr.Height * ((int)ButtonState - 1));
                 * graphics.Clear(this.Parent.BackColor);
                 * if (imageStrip != null)
                 * {
                 *      Bitmap bmp = imageStrip.Clone(sr, imageStrip.PixelFormat);
                 *      if (this.IsDesignMode() || !DesktopWindowManager.IsCompositionEnabled())
                 *      {
                 *              base.ImageList.Draw(graphics, bounds.X, bounds.Y, bounds.Width, bounds.Height, ((int)ButtonState - 1));
                 *      }
                 *      else
                 *      {
                 *              VisualStyleRendererExtender.DrawGlassImage(null, graphics, bounds, bmp);
                 *      }
                 * }
                 * else
                 *      using (Brush br = new SolidBrush(this.BackColor))
                 *              graphics.FillRectangle(br, sr);*/
            }
        }
        protected override void PaintButton(Graphics graphics, Rectangle bounds)
        {
            if (Application.RenderWithVisualStyles || DesktopWindowManager.IsCompositionEnabled())
            {
                try
                {
                    VisualStyleRenderer rnd = new VisualStyleRenderer(StyleClass, StylePart, (int)ButtonState);
                    if (this.IsDesignMode() || !DesktopWindowManager.IsCompositionEnabled())
                    {
                        rnd.DrawParentBackground(graphics, this.Bounds, this);
                        rnd.DrawBackground(graphics, this.Bounds, bounds);
                    }
                    else
                    {
                        rnd.DrawGlassBackground(graphics, this.Bounds, bounds);
                    }
                    return;
                }
                catch { }
            }

            //base.PaintButton(graphics, bounds);
            Rectangle sr = this.ClientRectangle;

            sr.Offset(0, sr.Height * ((int)ButtonState - 1));
            graphics.Clear(this.Parent.BackColor);
            if (imageStrip != null)
            {
                graphics.DrawImage(imageStrip, this.Bounds, sr, GraphicsUnit.Pixel);
            }
            else
            {
                using (Brush br = new SolidBrush(this.BackColor))
                    graphics.FillRectangle(br, sr);
            }
        }
        /// <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);
            }
        }