コード例 #1
0
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, bool up)
        {
            PushButtonState pbState = DetermineState(up);

            // First handle transparent case

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

            ButtonRenderer.DrawButtonForHandle(
                e,
                Control.ClientRectangle,
                false,
                pbState,
                DpiHelper.IsScalingRequirementMet ? Control.HandleInternal : IntPtr.Zero);

            // 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  isHighContrastHighlighted = up && IsHighContrastHighlighted();
                Color color = isHighContrastHighlighted ? SystemColors.Highlight : Control.BackColor;

                if (color.HasTransparency())
                {
                    using Brush brush = new SolidBrush(color);
                    e.GraphicsInternal.FillRectangle(brush, bounds);
                }
                else
                {
                    using var hdc = new DeviceContextHdcScope(e);
                    hdc.FillRectangle(
                        bounds,
                        isHighContrastHighlighted
                            ? User32.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF)
                            : Control.BackColorBrush);
                }
            }

            // This code is mostly taken from the non-themed rendering code path.
            if (Control.BackgroundImage != null && !DisplayInformation.HighContrast)
            {
                ControlPaint.DrawBackgroundImage(
                    e.GraphicsInternal,
                    Control.BackgroundImage,
                    Color.Transparent,
                    Control.BackgroundImageLayout,
                    Control.ClientRectangle,
                    bounds,
                    Control.DisplayRectangle.Location,
                    Control.RightToLeft);
            }
        }
コード例 #2
0
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds)
        {
            PushButtonState state = State;

            if (ButtonRenderer.IsBackgroundPartiallyTransparent(state))
            {
                ButtonRenderer.DrawParentBackground(e.Graphics, bounds, this);
            }
            ButtonRenderer.DrawButton(e.Graphics, ClientRectangle, false, state);

            if (!UseVisualStyleBackColor)
            {
                if (Application.RenderWithVisualStyles)
                {
                    bounds.Inflate(-BorderSize, -BorderSize);
                }
                else
                {
                    bounds.X      += 1;
                    bounds.Y      += 1;
                    bounds.Width  -= 3;
                    bounds.Height -= 3;
                }

                Color backColor = BackColor;
                if (backColor.A > 0)
                {
                    if (backColor.A == 0xff)
                    {
                        backColor = e.Graphics.GetNearestColor(backColor);
                    }
                    using (Brush brush = new SolidBrush(backColor))
                    {
                        e.Graphics.FillRectangle(brush, bounds);
                    }
                }
            }
        }
コード例 #3
0
        private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, bool up)
        {
            PushButtonState state = this.DetermineState(up);

            if (ButtonRenderer.IsBackgroundPartiallyTransparent(state))
            {
                ButtonRenderer.DrawParentBackground(e.Graphics, bounds, base.Control);
            }
            ButtonRenderer.DrawButton(e.Graphics, base.Control.ClientRectangle, false, state);
            bounds.Inflate(-ButtonBaseAdapter.buttonBorderSize, -ButtonBaseAdapter.buttonBorderSize);
            if (!base.Control.UseVisualStyleBackColor)
            {
                bool  flag      = false;
                Color backColor = base.Control.BackColor;
                if (((backColor.A == 0xff) && (e.HDC != IntPtr.Zero)) && (DisplayInformation.BitsPerPixel > 8))
                {
                    System.Windows.Forms.NativeMethods.RECT rect = new System.Windows.Forms.NativeMethods.RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
                    System.Windows.Forms.SafeNativeMethods.FillRect(new HandleRef(e, e.HDC), ref rect, new HandleRef(this, base.Control.BackColorBrush));
                    flag = true;
                }
                if (!flag && (backColor.A > 0))
                {
                    if (backColor.A == 0xff)
                    {
                        backColor = e.Graphics.GetNearestColor(backColor);
                    }
                    using (Brush brush = new SolidBrush(backColor))
                    {
                        e.Graphics.FillRectangle(brush, bounds);
                    }
                }
            }
            if ((base.Control.BackgroundImage != null) && !DisplayInformation.HighContrast)
            {
                ControlPaint.DrawBackgroundImage(e.Graphics, base.Control.BackgroundImage, Color.Transparent, base.Control.BackgroundImageLayout, base.Control.ClientRectangle, bounds, base.Control.DisplayRectangle.Location, base.Control.RightToLeft);
            }
        }
コード例 #4
0
        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.IsNull)
                {
                    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(
                            e,
                            ref r,
                            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);
            }
        }
コード例 #5
0
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Image imagestripe = Image;

            if (NumImages == 1 || imagestripe == null)
            {
                base.OnPaint(pevent);
            }
            else
            {
                Color transparentColor = Color.LightGray;
                Size  imgsize          = new Size(imagestripe.Width / NumImages, imagestripe.Height);
                Point imgstart         = new Point();
                if (imagestripe is Bitmap)
                {
                    transparentColor = ((Bitmap)imagestripe).GetPixel(0, imgsize.Height - 1);
                }

                PushButtonState state = DetermineState();
                if (NumImages >= 2 && state == PushButtonState.Disabled)
                {
                    imgstart.X = imgsize.Width;
                }
                else if (NumImages >= 3 && state == PushButtonState.Pressed)
                {
                    if (!Checked)
                    {
                        imgstart.X = imgsize.Width * 2;
                    }
                    else if (NumImages >= 4)
                    {
                        imgstart.X = imgsize.Width * 3;
                    }
                }

                Rectangle destRect = ClientRectangle;
                if (ButtonRenderer.IsBackgroundPartiallyTransparent(state))
                {
                    ButtonRenderer.DrawParentBackground(pevent.Graphics, destRect, this);
                }
                ButtonRenderer.DrawButton(pevent.Graphics, destRect, false, state);
                InitializeRenderer((int)state);
                destRect = visualStyleRenderer.GetBackgroundContentRectangle(pevent.Graphics, destRect);

                Padding imgmargin = Padding;
                destRect.X      += imgmargin.Left;
                destRect.Width  -= imgmargin.Horizontal;
                destRect.Y      += imgmargin.Top;
                destRect.Height -= imgmargin.Vertical;
                destRect         = Align(imgsize, destRect, ImageAlign);

                using (ImageAttributes attr = new ImageAttributes())
                {
                    if (transparentColor.A == 255)
                    {
                        attr.SetColorKey(transparentColor, transparentColor);
                    }
                    pevent.Graphics.DrawImage(imagestripe, destRect, imgstart.X, imgstart.Y, imgsize.Width, imgsize.Height, GraphicsUnit.Pixel, attr);
                }
            }
        }
コード例 #6
0
ファイル: ButtonStandardAdapter.cs プロジェクト: ash2005/z
        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

            ButtonRenderer.DrawButton(e.Graphics, Control.ClientRectangle, false, pbState);

            // 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.
            //HACK We need to see what's best here.  changing this to HACK 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;
                Color color   = 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)
                    {
                        NativeMethods.RECT r = new NativeMethods.RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
                        SafeNativeMethods.FillRect(new HandleRef(e, e.HDC), ref r, new HandleRef(this, 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);
            }
        }