VS 2012扩展中的标准控件

Flo*_*her 8 wpf vs-extensibility visual-studio-2012

我目前正在更改公司内部的VS扩展以支持Visual Studio 2012.我正在努力的是如何使UI动态地适应活动的VS主题.

我找到了几个颜色/画笔的资源键(Microsoft.VisualStudio.Shell.11.0.dll中的VsColors/VsBrushes),我可以轻松地使用它来更改扩展的基本颜色方案.问题是标准控件(文本框,组合框,复选框)具有默认的WPF外观,看起来很奇怪.

所以问题是:是否有可能在VS扩展的WPF工具窗口中制作标准控件看起来与Visual Studio中使用的类似?我知道我可以自己使用控件模板或自定义控件来做到这一点,但我真的想在某种程度上避免这种努力.

Mik*_*kov 7

Visual Studio 2012使用自定义WPF控件.您可以通过Snoop自行验证.Visual Studio 2012的WPF可视树包含如下控件Microsoft.VisualStudio.PlatformUI.VsButton, Microsoft.VisualStudio.PlatformUI.Shell.Controls.TabGroupControl, Microsoft.VisualStudio.PlatformUI.SearchControl.遗憾的是,这些控件没有记录,很难或不可能重复使用.您只能查看复杂元素的样式,并在代码中实现类似.

我基于WinfriedLö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();
  }
}
Run Code Online (Sandbox Code Playgroud)

要正确反转,Light主题的图标应该是另一个Visual Studio图标(灰色边缘,就像这样 错误图标).