Standard controls in VS 2012 extension

I am currently modifying the internal VS extension to support Visual Studio 2012. I am struggling with how to dynamically adapt the user interface to the active VS theme.

I found several resource keys for colors / brushes (VsColors / VsBrushes in Microsoft.VisualStudio.Shell.11.0.dll) that I can easily use to change the base color scheme of the extension. The problem is that standard controls (text fields, combo boxes, check boxes) have the default WPF look, which looks really weird.

So the question is: is it possible to make the standard controls in the WPF VS extension tool window look similar to those used in Visual Studio? I know that I could do it myself using control templates or custom controls, but I really want to avoid this effort if possible.

+7
source share
2 answers

Visual Studio 2012 uses custom WPF controls. You can check it yourself Snoop . The Visual Studio 2012 WPF visual tree contains controls such as Microsoft.VisualStudio.PlatformUI.VsButton, Microsoft.VisualStudio.PlatformUI.Shell.Controls.TabGroupControl, Microsoft.VisualStudio.PlatformUI.SearchControl . Unfortunately, these controls are not documented and are difficult or impossible to reuse. You can only view styles of complex elements and implement them in your code.

I am creating a similar management base for the Winfried LΓΆtzsch Collection (now it is included in the MahApps.Metro Toolkit ). I also saw another collection of attractive elements. This may be helpful.

To implement support for Visual Studio themes, I use resources from Microsoft.VisualStudio.Shell.VsBrushes/VsColors and my own colors. To convert the icons to the current theme, I use the following code:

 private readonly IVsUIShell5 _vsUIShell5; private string _currentThemeId; // cache icons for specific themes: <<ThemeId, IconForLightTheme>, IconForThemeId> private readonly Dictionary<Tuple<string, BitmapImage>, BitmapImage> _cacheThemeIcons = new Dictionary<Tuple<string, BitmapImage>, BitmapImage>(); protected override BitmapImage GetIconCurrentTheme(BitmapImage iconLight) { Debug.Assert(iconLight != null); return _currentThemeId.ToThemesEnum() == Themes.Light ? iconLight : GetCachedIcon(iconLight); } private BitmapImage GetCachedIcon(BitmapImage iconLight) { BitmapImage cachedIcon; var key = Tuple.Create(_currentThemeId, iconLight); if (_cacheThemeIcons.TryGetValue(key, out cachedIcon)) { return cachedIcon; } var backgroundColor = FindResource<Color>(VsColors.ToolWindowBackgroundKey); cachedIcon = CreateInvertedIcon(iconLight, backgroundColor); _cacheThemeIcons.Add(key, cachedIcon); return cachedIcon; } private BitmapImage CreateInvertedIcon(BitmapImage inputIcon, Color backgroundColor) { using (var bitmap = inputIcon.ToBitmapByPngEncoder()) { var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var bitmapData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat); var sourcePointer = bitmapData.Scan0; var length = Math.Abs(bitmapData.Stride) * bitmap.Height; var outputBytes = new byte[length]; Marshal.Copy(sourcePointer, outputBytes, 0, length); _vsUIShell5.ThemeDIBits((UInt32)outputBytes.Length, outputBytes, (UInt32)bitmap.Width, (UInt32)bitmap.Height, true, backgroundColor.ToUInt()); Marshal.Copy(outputBytes, 0, sourcePointer, length); bitmap.UnlockBits(bitmapData); return bitmap.ToPngBitmapImage(); } } 

To invert correctly, the Light theme icon must be a different Visual Studio icon (with a gray border around, for example, error icon )

+6
source

Dll decompile

The Microsoft.VisualStudio.Shell.12.dll resources have themes/generic.baml that can contain styles for the controls you are looking for. I used dotPeek, but I do not have a plugin for rendering Baml files, there are several ways.

You should check if the license allows you to use the extracted styles, though = P.

+1
source

All Articles