문제

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 :)

도움이 되었습니까?

해결책 2

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.

다른 팁

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

I see no reason to use an icon, PNG, or any other image resource for this. That seems a complete waste of resources.

Just draw the boxes from code, drawing the colors that are currently part of the control's color list. Ideally, your control will allow the caller to add their own colors. And it wouldn't require any fiddling around with images.

I just used an owner-draw combo box when I did this, but I drew a color box as I'm describing. Very easy.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top