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
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
Hi Matt,
thanks a lot for your great help. It works like a charm, even when the filter may no increase the performance of my application. I only have to set the filter to one button, so this should be fine :)
Thanks,Stefan
I don't even know that there would be all that much of a performance impact to this particular DrawFilter anyway, since you're just drawing an area that either the button or the OS would be rendering. If you do want to keep this as efficient as possible, you could certainly cache the brushes and make sure that you dispose the appropriately.