How can I retreive system colors related to current Aero style? I especially need the colors used in the selection gradient. SystemColors structure does not contain required colors.

Aero colors

Alternatively: How can I use WinAPI to draw selection on the specific canvas (Graphics object)?

有帮助吗?

解决方案

OK, so the answer to the initial question is: there is no way to determine the specific colors I asked for. It is evaluated by internal theming routines provided by the OS.

Fortunately, there is a way to ask the OS to draw a themed piece of control, so called part. .NET provides two classes for drawing UI:

Selection I asked for may be drawn by the following code:

// Graphics g = ...
VisualStyleRenderer selectionRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Item.Selected);
selectionRenderer.DrawBackground(g, someRectangle);

Unfortunately, neither TreeView.Item.Selected, nor ListView.Item.Selected is supported by the default window theme. However, one may switch theme to the Explorer using WinAPI via p/invoke:

// C++
SetWindowTheme(hwnd, L"Explorer", nullptr);

And then P/invoke his way through few UXTheme.h routines, which works perfectly.

其他提示

The system-defined color of the background of the selected items which includes the selected text and selected menu items as well is located at System.Drawing.KnownColor.Highlight

You may then use the Color struct to get a color from a KnownColor

Example

System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Highlight);

Thanks,
Happy Holidays! :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top