Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
560
Custom BackGradientStyle for WinButton
posted

Hi there,

I want to create a winbutton that has two different backgroundcolors. There are a lot of backgradientstyles available to choose from, but there is nothing that fits for my need. I just want 50% of my button with backgroundcolor #1, the other 50% with backgroundcolor #2, like using vertical but without any gradient. Is it possible to extend the WinButton with more backgradientstyles?

Thanks in advance,
Stefan

Parents
No Data
Reply
  • 37774
    Verified Answer
    posted

     Stefan,

    You could accomplish this with a draw filter.  A quick implementation, with the amazing ability to make your eyes bleed when used on a button, would be the following:

     private class ButtonDrawFilter : IUIElementDrawFilter
    {
        public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
        {
            Rectangle elemRect = drawParams.Element.RectInsideBorders;
            Rectangle topRect = new Rectangle(elemRect.Location, new Size(elemRect.Width, elemRect.Height / 2));
            Rectangle bottomRect = new Rectangle(new Point(topRect.Left, topRect.Bottom), topRect.Size);

            using (SolidBrush brush1 = new SolidBrush(Color.Red))
            {
                drawParams.Graphics.FillRectangle(brush1, topRect);
            }

            using (SolidBrush brush2 = new SolidBrush(Color.Green))
            {
                drawParams.Graphics.FillRectangle(brush2, bottomRect);
            }

            return true;
        }

        public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)
        {
            if (drawParams.Element is UltraButtonUIElement)
                return DrawPhase.BeforeDrawBackColor;

            return DrawPhase.None;
        }
    }

    You would assign a new instance of this class to the DrawFilter property of the UltraButton.  You will likely also have to disable the UseOSThemes property as well.

    -Matt

Children