views:

49

answers:

2

You know those color picker buttons with a little rectangle in it, displaying the currently selected color? Like in MS Office products.

I would like to implement one using C# / .NET. So I've got a nice little icon with a magenta-colored rectangle (which is to display the color) and a transparent background.

I can think of two ways how this could be done, but they're both not really elegant:

  • Edit the icon using graphics software to have a solid background color instead of transparency and resize it to be exactly as large as the button containing it. This would allow to use TransparentColor=Manenta in combination with the button Background, however the icon would have to be edited whenever button size, toolbar BackColor or something else changes.
  • Edit the icon programmatically whenever a new color is selected. Would work, but seems a little bit to complex (regarding development and performance) to me.

So. Maybe I am missing the obvious, easy way to implement such a button? Thanks in advance for any hints/suggestions/inspiration :)

A: 

You can easily override the OnPaint method and draw a rectangle with any color you desire.

Joey
You're right, this wouldn't be too complex. But it would also mean changing the code when the icon changes :( With the Image#TransparentColor property and all those magenta-ed icons out there, I'd hope there was an elegant way to solve this (which I am just to blind to see, maybe) :-/
peter p
A: 

Obviously, there is no elegant solution. I chose the first one, using PNG transparency and BackColor, with the drawback of having to color the png background like the background of the containing panel. Seems to be least painful this way.

peter p