Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Use Microsoft.Windows.Shell instead of hand-coded solution for WinForms interop #503

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@
using System.Windows.Media;
using MahApps.Metro.Controls;
using MahApps.Metro.Native;
#if NET_4
using Microsoft.Windows.Shell;
#else
using System.Windows.Shell;
#endif

namespace MahApps.Metro.Behaviours
{
public class BorderlessWindowBehavior : Behavior<Window>
{
public static readonly DependencyProperty ResizeWithGripProperty = DependencyProperty.Register("ResizeWithGrip", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(true));
public static readonly DependencyProperty AutoSizeToContentProperty = DependencyProperty.Register("AutoSizeToContent", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(false));
public static readonly DependencyProperty EnableDWMDropShadowProperty =
DependencyProperty.Register("EnableDWMDropShadow", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(false, new PropertyChangedCallback((obj, args) =>
public static readonly DependencyProperty EnableDWMDropShadowProperty = DependencyProperty.Register("EnableDWMDropShadow", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(false));

public static readonly DependencyProperty AllowsTransparencyProperty =
DependencyProperty.Register("AllowsTransparency", typeof(bool), typeof(BorderlessWindowBehavior), new PropertyMetadata(true, new PropertyChangedCallback((obj, args) =>
{
var behaviorClass = ((BorderlessWindowBehavior)obj);

if (behaviorClass.AssociatedObject != null)
behaviorClass.AssociatedObject.AllowsTransparency = !(bool)args.NewValue;
behaviorClass.AssociatedObject.AllowsTransparency = (bool)args.NewValue;
})));

public bool AllowsTransparency
{
get { return (bool)GetValue(AllowsTransparencyProperty); }
set { SetValue(AllowsTransparencyProperty, value); }
}

public bool EnableDWMDropShadow
{
get { return (bool)GetValue(EnableDWMDropShadowProperty); }
Expand Down Expand Up @@ -84,8 +97,9 @@ protected override void OnAttached()
AssociatedObject.SourceInitialized += AssociatedObject_SourceInitialized;

AssociatedObject.WindowStyle = WindowStyle.None;
AssociatedObject.AllowsTransparency = !EnableDWMDropShadow;
AssociatedObject.AllowsTransparency = AllowsTransparency;
AssociatedObject.StateChanged += AssociatedObjectStateChanged;
AssociatedObject.SetValue(WindowChrome.GlassFrameThicknessProperty, new Thickness(-1));

if (AssociatedObject is MetroWindow)
{
Expand All @@ -97,6 +111,10 @@ protected override void OnAttached()
Border = ancestors;
if (ShouldHaveBorder())
AddBorder();
var titleBar = window.GetPart<Grid>("PART_TitleBar");
titleBar.SetValue(WindowChrome.IsHitTestVisibleInChromeProperty, true);
var windowCommands = window.GetPart<ContentPresenter>("PART_WindowCommands");
windowCommands.SetValue(WindowChrome.IsHitTestVisibleInChromeProperty, true);
};

switch (AssociatedObject.ResizeMode)
Expand Down Expand Up @@ -260,25 +278,22 @@ private IntPtr HwndHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam,
break;
case Constants.WM_NCPAINT:
{
if (!ShouldHaveBorder())
if (ShouldHaveBorder())
{
this.AddBorder();
}
else if (EnableDWMDropShadow)
{
MetroWindow w = AssociatedObject as MetroWindow;
if (!(w != null && w.GlowBrush != null))
var metroWindow = AssociatedObject as MetroWindow;
if (!(metroWindow != null && metroWindow.GlowBrush != null))
{
var val = 2;
UnsafeNativeMethods.DwmSetWindowAttribute(_mHWND, 2, ref val, 4);
var m = new MARGINS { bottomHeight = 1, leftWidth = 1, rightWidth = 1, topHeight = 1 };
UnsafeNativeMethods.DwmExtendFrameIntoClientArea(_mHWND, ref m);
}

// i think we don't need this, cause after minimizing on taskbar, no border is shown
//if (Border != null)
//Border.BorderThickness = new Thickness(0);
}
else
{
AddBorder();
}

handled = true;
}
break;
Expand Down
9 changes: 6 additions & 3 deletions MahApps.Metro/MahApps.Metro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;NET_4</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;NET_4</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand All @@ -43,6 +43,9 @@
<AssemblyOriginatorKeyFile>mahapps.metro.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Windows.Shell">
<HintPath>..\packages\Microsoft.Windows.Shell.3.0.1.0\lib\net40\Microsoft.Windows.Shell.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -441,4 +444,4 @@
</Target>
-->
<Target Name="AfterCompile" />
</Project>
</Project>
5 changes: 5 additions & 0 deletions MahApps.Metro/MahApps.Metro.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
<file src="bin\Release\NET45\System.Windows.Interactivity.dll" target="lib\net45\System.Windows.Interactivity.dll" />
<file src="tools\install.ps1" target="tools\install.ps1" />
</files>
<dependencies>
<group targetFramework="net40">
<dependency id="Microsoft.Windows.Shell" version="3.0.1.0" />
</group>
</dependencies>
</package>
1 change: 1 addition & 0 deletions MahApps.Metro/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Windows.Shell" version="3.0.1.0" targetFramework="net40-Client" />
<package id="MSBuildTasks" version="1.4.0.45" targetFramework="net40-Client" />
</packages>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Microsoft.Windows.Shell</id>
<version>3.0.1.0</version>
<title>Microsoft.Windows.Shell</title>
<authors>Gareth Evans</authors>
<owners>Gareth Evans</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Provides Windows Chrome and Taskbar integration facilities</description>
<releaseNotes />
<copyright />
<language />
</metadata>
</package>
Binary file not shown.
Binary file not shown.
54 changes: 54 additions & 0 deletions samples/MetroDemo/InteropDemo.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<controls:MetroWindow x:Class="MetroDemo.InteropDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviours="clr-namespace:MahApps.Metro.Behaviours;assembly=MahApps.Metro"
Title="Interop Demo"
Height="800"
Width="800">
<i:Interaction.Behaviors>
<behaviours:BorderlessWindowBehavior ResizeWithGrip="True" AllowsTransparency="False" />
</i:Interaction.Behaviors>

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Border BorderBrush="{DynamicResource AccentColorBrush}" Margin="10" BorderThickness="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="you should see google below"
Grid.Row="0"
FontSize="24"
Foreground="{DynamicResource BlackBrush}"
HorizontalAlignment="Center" />
<WindowsFormsHost Grid.Row="1">
<winForms:WebBrowser Url="http://www.google.com" />
</WindowsFormsHost>
</Grid>
</Border>
<!--<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="SkyBlue"
BorderBrush="DeepSkyBlue"
BorderThickness="1"
Opacity="0.5">
<TextBlock Margin="50"
FontSize="24"
Text="Hello Airspace" />
</Border>-->
</Grid>
</controls:MetroWindow>
10 changes: 10 additions & 0 deletions samples/MetroDemo/InteropDemo.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MetroDemo
{
public partial class InteropDemo
{
public InteropDemo()
{
InitializeComponent();
}
}
}
3 changes: 2 additions & 1 deletion samples/MetroDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MahApps.Metro.Demo"
Width="800"
Width="960"
Height="600"
Icon="mahapps.metro.logo2.ico"
ShowIconOnTitleBar="true"
Expand Down Expand Up @@ -44,6 +44,7 @@
<Button Click="LaunchPanoramaDemo" Content="Panorama Demo" />
<Button Click="LaunchIcons" Content="Icons" />
<Button Click="LaunchRibbonDemo" Content="Ribbon Demo (.NET 4.5)" />
<Button Click="InteropDemo" Content="Interop Demo" />
</Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>

Expand Down
5 changes: 5 additions & 0 deletions samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,10 @@ private void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs
break;
}
}

private void InteropDemo(object sender, RoutedEventArgs e)
{
new InteropDemo().Show();
}
}
}
9 changes: 9 additions & 0 deletions samples/MetroDemo/MetroDemo.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Reference Include="PresentationFramework.Aero" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NET45\System.Windows.Interactivity.dll</HintPath>
Expand All @@ -79,6 +80,7 @@
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
Expand All @@ -91,6 +93,9 @@
<Compile Include="IconsWindow.xaml.cs">
<DependentUpon>IconsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="InteropDemo.xaml.cs">
<DependentUpon>InteropDemo.xaml</DependentUpon>
</Compile>
<Compile Include="MainWindowViewModel.cs" />
<Compile Include="Models\Album.cs" />
<Compile Include="Models\Artist.cs" />
Expand All @@ -106,6 +111,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="InteropDemo.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
9 changes: 9 additions & 0 deletions samples/MetroDemo/MetroDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Reference Include="PresentationFramework.Aero" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NET40\System.Windows.Interactivity.dll</HintPath>
Expand All @@ -65,6 +66,7 @@
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
Expand All @@ -77,6 +79,9 @@
<Compile Include="IconsWindow.xaml.cs">
<DependentUpon>IconsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="InteropDemo.xaml.cs">
<DependentUpon>InteropDemo.xaml</DependentUpon>
</Compile>
<Compile Include="MainWindowViewModel.cs" />
<Compile Include="Models\Album.cs" />
<Compile Include="Models\Artist.cs" />
Expand All @@ -92,6 +97,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="InteropDemo.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down