Visual Studio 2012 使用自定义 WPF 控件。您可以通过Snoop自己验证这一点。Visual Studio 2012 的 WPF 可视化树包含诸如Microsoft.VisualStudio.PlatformUI.VsButton, Microsoft.VisualStudio.PlatformUI.Shell.Controls.TabGroupControl, Microsoft.VisualStudio.PlatformUI.SearchControl
. 不幸的是,这些控件没有记录在案,很难或不可能重用。您只能查看复杂元素的样式并在代码中实现类似的样式。
我基于Winfried Lötzsch 集合创建了类似的控件(现在它包含在MahApps.Metro 工具包中)。我还看到了另一个吸引人的元素。它也可能有用。
为了实现对 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();
}
}
要正确反转,Light 主题的图标应该是另一个 Visual Studio 图标(周围有灰色边缘,像这样)。