Visual Studio 2012 使用自定义 WPF 控件。您可以通过以下方式自行验证Snoop http://snoopwpf.codeplex.com/。 Visual Studio 2012 的 WPF 可视化树包含以下控件Microsoft.VisualStudio.PlatformUI.VsButton, Microsoft.VisualStudio.PlatformUI.Shell.Controls.TabGroupControl, Microsoft.VisualStudio.PlatformUI.SearchControl
。不幸的是,这些控件没有记录在案,并且很难或不可能重用。您只能查看复杂元素的样式并在代码中实现类似的样式。
我基于创建类似的控件温弗里德·洛茨奇系列 http://www.codeproject.com/Articles/442856/Visual-Studio-2012-Metro-Styles-for-WPF(现在它已包含在MahApps.Metro 工具包 https://github.com/MahApps/MahApps.Metro)。我也看到了另一个集合 http://elysium.asvishnyakov.com/的吸引力元素。它可能也有用。
为了实现对 Visual Studio 主题的支持,我使用了来自Microsoft.VisualStudio.Shell.VsBrushes/VsColors
和自己的颜色。要将图标转换为当前主题,我使用以下代码:
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 inverted correctly, the icon of Light theme should be as another Visual Studio icons (with gray rim around, like this ).