OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Hi there,
Windows 10
I have created a right-click context menu (CreateMenu Win32 API) with 3 menu entries\items. The 3 menu items have all a light-grey background color but, when I point the mouse pointer over any of the menu items, the background of the menu item that is currently under the mouse becomes highlighted (becomes hot) and its color changes to light-blue.
If I change the machine theme colors via control panel, the highlight color is no longer light-blue. It changes to dark-grey.
I need to retrieve this highlight color regardless of the current color theme.
GetSysColor doesn't work with a themed user interface.
I have done some googling and found that one should use the uxtheme library for retrieving this menu highlight color, namely the OpenThemeData and GetThemeColor apis.
Questions:
1- OpenThemeData takes a hwnd. I don't know which hwnd I should pass to this api given the fact that the context menu is for an added system tray icon. Should I pass the hwnd of the application or that of the system tray, the menu handle or some other hwnd?
2- GetThemeColor takes two arguments (Parts and States)... I am not sure which ones I should use... Should I use (MENU, MENU_POPUPITEM,MENU_POPUPBACKGROUND .... MBI_HOT, MPI_HOT)... etc ?
Thanks in advance.
Last edited by JAAFAR; Jul 27th, 2021 at 03:50 PM.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
GetSysColor() will not track with theme changes made since the last reboot.
GetSysColorBrush() will however, since that is what gets used.
System color brushes track changes in system colors. In other words, when the user changes a system color, the associated system color brush automatically changes to the new color.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
@wqweto
OpenThemeData should work fine with 0 for hWnd
Didn't work for me. I had to pass the menu handle in order for it to work.
Take a look at AeroStyle.xml for ideas about class names and part names.
I have tried many constant combinations but none of them give me the exact highlight color.
@dilettante.
Originally Posted by dilettante
GetSysColor() will not track with theme changes made since the last reboot.
GetSysColorBrush() will however, since that is what gets used.
Tried that already but, just like with GetSysColor, it would only give me the COLOR_MENU. It won't give me the highlight color that I see when selecting the menu item or when hovering it with the mouse.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Originally Posted by Steve Grant
I don't know if it might help but that colour is &HF7C991.
Thanks Steve ... That is indeed the correct color but I need to get it programmatically as the color varies from one user machine to another depending on the machine color theme currently applied.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Originally Posted by wqweto
There is a
<class name="CommunicationsStyle">
<part name="Tab">
<state name="Hot">
<AccentColorHint>148 201 254</AccentColorHint>
. . . in AeroStyle.xml above which is a very close &HFEC994 color hint.
cheers,
</wqw>
Hi wqweto,
You nailed it !!
I successfully read the correct color. (Note: This time, passing the menu handle in the OpenThemeData hwnd arg didn't work. I had to pass 0 like you suggested in an earlier post.
Relying on the class names as well as the part names is not intuitive at all ... I would have never guessed that CommunicationsStyle was the class I needed for menus... Constants not descriptive at all
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Originally Posted by dilettante
How are you using this color anyway?
Seems like it would require a lot of subclassing and bitmap swapping. Is it really worth all that?
What I have in mind is to make that color as the background color of the menu bitmap in the last argument of the SetMenuItemBitmaps API so that the bitmap background looks transparent when the user hovers the menu entry with the mouse.
Haven't tested the idea yet and I don't even know if this will work in the end but, in any case, even if this doesn't work, I still will have learnt a few interesting things in the process.
Last edited by JAAFAR; Jul 28th, 2021 at 07:55 PM.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Wrong call, that one only sets the checked and unchecked bitmaps. The one you want is SetMenuItemInfo() instead.
But in both cases the bitmaps are meant to be monochrome mask bitmaps. Using a color bitmap, the best you can do is use the menu color. Choosing the hover color will look really bad.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Originally Posted by dilettante
Wrong call, that one only sets the checked and unchecked bitmaps. The one you want is SetMenuItemInfo() instead.
But in both cases the bitmaps are meant to be monochrome mask bitmaps. Using a color bitmap, the best you can do is use the menu color. Choosing the hover color will look really bad.
Ok, I see ...Thanks for the advice.
I quick search on this api came up with this old code of yours. I will take a close look at it and see what I can learn from it.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
I'm not sure whether the corrected version ever got posted. Version 6 is the newest I have here.
I have another example that is very stripped down, just a Form with all of the giblets in it instead of a Class to hold the fiddly bits. I can tell you now that since it uses a color bitmap it will not have the look you want when selected though. However it does track theme changes for the "non-hover" menu background color.
If you figure that out, then great. But as the documentation says, these are supposed to be monochrome mask bitmaps and color bitmaps "may give crappy results."
The selected and clear bitmaps should be monochrome. The system uses the Boolean AND operator to combine bitmaps with the menu so that the white part becomes transparent and the black part becomes the menu-item color. If you use color bitmaps, the results may be undesirable.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Originally Posted by dilettante
I'm not sure whether the corrected version ever got posted. Version 6 is the newest I have here.
I have another example that is very stripped down, just a Form with all of the giblets in it instead of a Class to hold the fiddly bits. I can tell you now that since it uses a color bitmap it will not have the look you want when selected though. However it does track theme changes for the "non-hover" menu background color.
If you figure that out, then great. But as the documentation says, these are supposed to be monochrome mask bitmaps and color bitmaps "may give crappy results."
Hi dilettante,
Thanks for taking the time to help. I will take a look at the example and see if I can adapt it to my needs.
Re: OpenThemeData For Getting Background Color Of Context Menu Item ? (uxtheme.dll)
Test it first. It should run fine in the IDE.
I do not think it will do exactly what you want though. I can't think of any way to avoid showing the menu background color in the square of a color bitmap when you hover over the item. I.e. it is not "transparent."
The only thing I can imagine might be to use subclassing to detect hovering and replace the bitmap again with one painted on the hover-color background, then flip back after hovering moves on. If you do that you'll probably have to call DrawMenuBar() each time you change bitmaps.