What is MFC's algorithm or function for calculating controls' highlight, shadow, etc.?

175 views Asked by At

I've made a ColorButton subclass of CButton, setting BS_OWNERDRAW flag in styles.

It works fine: I can set its text, background, etc. etc.

However I don't want to have to set the minor colors manually (highlight, shadow, etc.) I have a heuristic to choose white or black for text based on background. I could do the same for the other minor colors but my formulas would differ from what Windows would do were the same color the background, and thus look odd. So:

Question: Is there any way to find out what Windows would return for GetSysColor(COLOR_BTNSHADOW) with a given background color?

To anticipate an answer I see CMFCButton allows custom colors without making a whole subclass. What I've seen again allows the micromanagement of setting every color, but not the high-level ability I'm looking for. I'd may rather stick with my solution as it's already running, and works on XP and later. (This is a freeware utility so who knows what old OS users might have.) That said, If there's a CMFCButton solution that would also be of interest.

2

There are 2 answers

3
SoronelHaetir On

GetSysColor is not sensitive to the background color. It simply has its set of color entries (based on the visual style) and will return those values. If you want values that make sense for some other background you will need to come up with reasonable values yourself.

0
Swiss Frank On

Not a complete answer at all, but as long as the color is a monochrome (with R G and B components equal):

  • the highlight color is 127 + channelvalue / 2 (integer math)

  • the shadow color is channelvalue / 3 * 2 (again integer math)

Example: a background with R, G, and B values of 200 would have a highlight of 227 and a shadow of 132.

For non-gray backgrounds, a quite different calculation seems to take place. I'm getting very usable results simply using the above formulas channel by channel, but the original question isn't "what would give usable results" but specifically "what is Windows' calculation?"

Windows also has a dark shadow color which is invariably 0x404040 in every color I've tried.