I am currently changing a company-internal VS extension to support Visual Studio 2012. What I am struggling with is how to make the UI adapting to the active VS theme dynamically.
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 basic color scheme of the extension. The problem is that the standard controls (text boxes, combo boxes, check boxes) have the default WPF appearance which looks really weird.
So the question is: Is there any possibility to make standard controls in a WPF tool window of a VS extension look similar to the ones used in Visual Studio? I am aware that I could do this myself using control templates or custom controls but I really want to avoid this effort if somehow possible.
Visual Studio 2012 use custom WPF controls. You can verify this yourself by Snoop. WPF visual tree of Visual Studio 2012 contains such controls 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 similar in your code.
I create similar controls base on Winfried Lötzsch collection (now it is included in the MahApps.Metro toolkit). I also saw another collection of attractive elements. It may be useful too.
To implement support for Visual Studio themes, I use resources from Microsoft.VisualStudio.Shell.VsBrushes/VsColors and own colors. To convert icons to current theme, I use 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 inverted correctly, the icon of Light theme should be as another Visual Studio icons (with gray rim around, like this ).
There is a themes/generic.baml in the resources of Microsoft.VisualStudio.Shell.12.dll that might contain the styles for the controls you are looking for. I used dotPeek but I don't have a plugin installed to visualize the Baml files, there a few you can try.
You should check if the license allows you to use the extracted styles though =P.
Related
I'm porting a WPF app to WPF .NET Core 3.0. I'm trying to use UWP controls using XAML Islands via WindowsXamlHost from the Community Toolkit. The WindowsXamlHost control itself has a white background and I don't know how to get rid of it. Here's an example of a ProgressRing:
<xamlHost:WindowsXamlHost x:Name="MyRing" InitialTypeName="Windows.UI.Xaml.Controls.ProgressRing"/>
private void MyRing_ChildChanged(object sender, EventArgs e)
{
if (MyRing.Child is ProgressRing progressRing)
{
progressRing.IsActive = true;
var brush = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Transparent);
progressRing.Background = brush;
}
}
Giving the ProgressRing object a new background color works but doesn't help if I'm after transparency. I'm using .NET Core 3 preview 7.
I'm not sure whether it is possible. I have a similar issue which I reported here: https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/issues/160#issuecomment-522288462. Hopefully, Microsoft employees will answer there.
In Visual Studio 2008, when I add some controls to form the designer creates some codes regarding the properties of the control automatically. Now, I'm using my own user controls and by adding them to the form, the designer again creates the code lines automatically, in this case the property FONT is one of those that I don't want the designer to add it since it overwrites the font setting in the upper level. Anyone knows how I can set which properties to be set in designer?
The designer only adds a line of code changing a property's value if it determines that the value is different from the DefaultValue[Attribute].
If your custom control wants to change what the default value of the Font property is (or any other base-class property), you have to perform a little wizardry:
public class MyControl : Control
{
public MyControl()
{
base.Font = new Font("Arial", 9.75f);
}
[DefaultValue(typeof(Font), "Arial, 9.75pt")]
public new Font Font
{
get { return base.Font; }
set { base.Font = value; }
}
}
Notice the 'new' keyword on the Font property? Font is not virtual, so you can't override it and we don't want to do that. You override to change behavior. We don't want to alter the behavior (which is why the code simply redirects back to the base), we just want to expose a new DefaultValue. This tricks the designer into considering the new default for your control.
We also make sure that our Font property has that value when it is constructed.
I'm new to working with Visual Studio 2010.
I want to know how to set the background colour of a form to be a gradient colour in c++.
I saw some source online but that was for Visual Basic .NET.
appreciate your help :)
You are going to have to get used to finding .NET sample code in vb.net or C# syntax, writing Winforms code in C++/CLI is not often done. The translation is rather mechanical so be sure to get a decent on book on C++/CLI programming so you can just rattle it off yourself.
Anyhoo, the code is pretty simple, just override the OnPaintBackground method and modify the constructor so the form will redraw itself whenever the size is changed:
protected:
virtual void OnPaintBackground(PaintEventArgs^ e) override {
System::Drawing::Drawing2D::LinearGradientBrush brush(Point::Empty, Point(this->ClientSize.Width, this->ClientSize.Height), Color::Yellow, Color::Blue);
e->Graphics->FillRectangle(%brush, 0, 0, this->ClientSize.Width, this->ClientSize.Height);
}
Constructor:
Form1(void) {
InitializeComponent();
SetStyle(ControlStyles::ResizeRedraw, true);
}
The current version of the Microsoft Live Labs PivotViewer control for SilverLight 4 has no way to style the elements of the control. Looking at the control in Reflector, I can see much of the style info is set in a ResourceDictionary in the assembly (assets/defaultcolors.xaml). What I would like to do is create my own copy of this file, then replace it at runtime on the PivotViewer control.
By subclassing the PivotViewer control and overriding OnApplyTemplate I can grab the child elements and set properties such as Background. I have not had any success Clear()'ng the MergedDictionaries and adding in my own:
public override void OnApplyTemplate() {
base.OnApplyTemplate();
/* can change things this way */
CollectionViewerView cvv = ((CollectionViewerView)((Grid)this.GetTemplateChild("PART_Container")).Children[0]);
((Grid)cvv.Content).Background = new SolidColorBrush(Colors.Black);
/* can't change things this way */
CustomDictionary gd = new CustomDictionary();
cvv.Resources.MergedDictionaries.Clear();
cvv.Resources.MergedDictionaries.Add(gd);
}
I'm afraid this isn't going to work in Silverlight because it uses only Static Resources. ( Styles Don't Update )
Changing a resource dictionary only works before InitializeComponent() is called, which is called in the constructor of the PivotViewer.
I've been trying to style the PivotViewer Control too. I hope there is another way, besides searching through the Visual Tree and changing properties.
I have an ActiveX control (written in Delphi) which I want to host in a WPF application. When I try to load it into the toolbox to add it to the XAML at design time, it is not shown in the list of available controls. Does anyone know what filters this list and why I can't see the control to add it?
Edit
This is where I get to - the host.Child = (ax); statement gets an error (Cannot implicitly convert type 'DemoFrameControl.DemoFrameCtrl' to 'System.Windows.Forms.Control'), hope this helps clarify my problem
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the ActiveX control.
DemoFrameControl.DemoFrameCtrl ax = new DemoFrameControl.DemoFrameCtrl();
// Assign the ActiveX control as the host control's child.
host.Child = (ax);
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
// Play a .wav file with the ActiveX control.
//axWmp.URL = #"C:\WINDOWS\Media\Windows XP Startup.wav";
}
Thanks
Check out Walkthrough: Hosting an ActiveX Control in WPF.
Update:
How is DemoFrameCtrl defined? Like the error says, it needs to be a subclass of System.Windows.Forms.Control to use WindowsFormsHost. An ActiveX control wrapper will inherit from AxHost which inherits from Control. I think Visual Studio will generate the wrapper if you add a reference to the ActiveX library. If not, you can try using Aximp.exe (Windows Forms ActiveX Control Importer).