2

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

Spook
  • 25,318
  • 18
  • 90
  • 167
  • It doesn't have anything to do with a selection. The way to draw this progress bar is shown in this answer: http://stackoverflow.com/a/2671848/17034 – Hans Passant Dec 26 '12 at 11:15
  • I don't really care about the progressbar, however I do care about the selection rectangle, as I stated in question. The VisualStyles is what I'm searching for, however I've got some trouble while using them, but I'll describe it in another question. – Spook Dec 26 '12 at 11:20
  • Shame, that you answer questions in comments, I cannot accept them. – Spook Dec 26 '12 at 11:33
  • 1
    Okay, you are talking about the pale blue background. That's the selection color for a ListView in tile mode, switched to the "EXPLORER" visual style with SetWindowTheme(). This is all rather distant from what you seem to want to do. You can post your own answer and accept it. – Hans Passant Dec 26 '12 at 11:48
  • The truth is, that I've tried to draw the treeview item's selection, but on the way I noticed, that listview has the same kind of selection. A little bit around, I admit, I would rather draw the original treeview's selection, if possible. I usually write and accept answers myself, but it seems unfair, as these are not my solutions :) – Spook Dec 26 '12 at 11:57
  • Can you tell me, what do you mean by "EXPLORER" visual style? Maybe you know how to access the renderer for treeview or listview selection? – Spook Dec 26 '12 at 12:12

2 Answers2

1

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.

Spook
  • 25,318
  • 18
  • 90
  • 167
  • I know that, I asked another question regarding this matter. I guess, that some additional conditions shall be met to use these parts and states. However, I think, that this way is the best to follow, if one wishes to draw default UI parts. – Spook Dec 26 '12 at 12:00
0

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

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • The Highlight in KnownColor is the same color, as SystemColors.Highlight and defines the color of a highlight, when the visual styles are disabled (as in Windows Classic style). This is not what I'm searching for. – Spook Dec 26 '12 at 11:35