Default styles offered for the Windows Forms context menu control (using ContextMenuStrip
) are looking like something that was created for Office XP - its styling doesn't quite match that of Windows 11.
For contrast, a modern Windows 11 context menu has rounded corners and is theme-aware:
Even for cases where it is not theme-aware for some legacy applications, it's still using rounded corners.
How can I replicate the theme-aware style and the modern (rounded corner) context menu for a Windows 11 tray icon from a Windows Forms (or Console) application?
Ideally, I am trying not to write a whole tray menu renderer from scratch and instead just re-use built-in OS components, but so far I am running into a wall when it comes to figuring out what tooling I need to use to even get the menu.
Use this class
class CustomContextMenu : ContextMenuStrip
{
[DllImport("dwmapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern long DwmSetWindowAttribute(IntPtr hwnd,
DWMWINDOWATTRIBUTE attribute,
ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
uint cbAttribute);
public CustomContextMenu()
{
var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND; //change as you want
DwmSetWindowAttribute(Handle,
DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE,
ref preference,
sizeof(uint));
}
public enum DWMWINDOWATTRIBUTE
{
DWMWA_WINDOW_CORNER_PREFERENCE = 33
}
public enum DWM_WINDOW_CORNER_PREFERENCE
{
DWMWA_DEFAULT = 0,
DWMWCP_DONOTROUND = 1,
DWMWCP_ROUND = 2,
DWMWCP_ROUNDSMALL = 3,
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.