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

Basic Wpf UserControl #1

Closed
wants to merge 14 commits into from
Closed
11 changes: 11 additions & 0 deletions LibVLCSharp.WPF/ForegroundWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Window x:Class="LibVLCSharp.WPF.ForegroundWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LibVLCSharp.WPF"
x:ClassModifier="internal"
mc:Ignorable="d"
Title="LibVLCSharp.WPF" Height="300" Width="300" WindowStyle="None" Background="Transparent" ResizeMode="NoResize" AllowsTransparency="True" ShowInTaskbar="False">
<Grid x:Name="PART_Content" />
</Window>
106 changes: 106 additions & 0 deletions LibVLCSharp.WPF/ForegroundWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LibVLCSharp.WPF
{
internal partial class ForegroundWindow : Window
{
private Window wndhost;
private ContentControl bckgnd;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use "_" consistently for private fields please.

private UIElement _content;

internal new UIElement Content
{
get { return _content; }
set
{
_content = value;
if (_content != null)
{
PART_Content.Children.Clear();
PART_Content.Children.Add(_content);
}
}
}

internal ForegroundWindow(ContentControl Background)
{
InitializeComponent();
bckgnd = Background;
bckgnd.Loaded += Bckgnd_Loaded;
bckgnd.Unloaded += Bckgnd_Unloaded;
}

private void Bckgnd_Unloaded(object sender, RoutedEventArgs e)
{
wndhost.Closing -= Wndhost_Closing;
wndhost.SizeChanged -= Wndhost_SizeChanged;
wndhost.LocationChanged -= Wndhost_LocationChanged;
this.Hide();
}

private void Bckgnd_Loaded(object sender, RoutedEventArgs e)
{
wndhost = Window.GetWindow(bckgnd);
this.Owner = wndhost;
wndhost.Closing += Wndhost_Closing;
wndhost.SizeChanged += Wndhost_SizeChanged;
wndhost.LocationChanged += Wndhost_LocationChanged;
try
{
Point locationFromScreen = bckgnd.PointToScreen(new Point(0, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Point(0, 0) allocation can be cached in a readonly field.

PresentationSource source = PresentationSource.FromVisual(wndhost);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
this.Left = targetPoints.X;
this.Top = targetPoints.Y;
Vector size = bckgnd.PointToScreen(new Point(bckgnd.ActualWidth, bckgnd.ActualHeight)) - bckgnd.PointToScreen(new Point(0, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Point(0, 0) allocation can be cached in a readonly field.

this.Height = size.Y;
this.Width = size.X;
this.Show();
wndhost.Focus();
}
catch
{
this.Hide();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log an error or something? What could possibly happen here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure when this actually can happen. Most likely would only happen, if no Window with Hwnd is provided, but then you wouldn't see anything anyway

}
}

private void Wndhost_LocationChanged(object sender, EventArgs e)
{
Point locationFromScreen = bckgnd.PointToScreen(new Point(0, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Point(0, 0) allocation can be cached in a readonly field.

PresentationSource source = PresentationSource.FromVisual(wndhost);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
this.Left = targetPoints.X;
this.Top = targetPoints.Y;
}

private void Wndhost_SizeChanged(object sender, SizeChangedEventArgs e)
{
Point locationFromScreen = bckgnd.PointToScreen(new Point(0, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Point(0, 0) allocation can be cached in a readonly field.

PresentationSource source = PresentationSource.FromVisual(wndhost);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
this.Left = targetPoints.X;
this.Top = targetPoints.Y;
Vector size = bckgnd.PointToScreen(new Point(bckgnd.ActualWidth, bckgnd.ActualHeight)) - bckgnd.PointToScreen(new Point(0, 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new Point(0, 0) allocation can be cached in a readonly field.

this.Height = size.Y;
this.Width = size.X;
}

private void Wndhost_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Close();
}
}
}
23 changes: 23 additions & 0 deletions LibVLCSharp.WPF/Styles/VideoView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:LibVLCSharp.WPF">

<Style x:Key="VideoViewStyle" TargetType="{x:Type local:VideoView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:VideoView}">
<Grid >
<DockPanel LastChildFill="true">
<WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="PART_PlayerHost">
<wf:Panel Dock="Fill" x:Name="PART_PlayerView" />
</WindowsFormsHost>
</DockPanel>
<ContentControl Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
99 changes: 99 additions & 0 deletions LibVLCSharp.WPF/VideoView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LibVLCSharp.WPF
{
public partial class VideoView : UserControl, LibVLCSharp.Shared.IVideoView, IDisposable
{
private LibVLCSharp.Shared.MediaPlayer mediaPlayer;
private LibVLCSharp.Shared.LibVLC libVLC;
private double controlWidth;
private double controlHeight;
private ForegroundWindow foreground;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please use "_" for consistent code styling ;-)


public VideoView()
{
ResourceDictionary res = Application.LoadComponent(new Uri("/LibVLCSharp.WPF;component/Styles/VideoView.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;
this.Style = res["VideoViewStyle"] as Style;

foreground = new ForegroundWindow(this);

Shared.Core.Initialize();
libVLC = new Shared.LibVLC();
mediaPlayer = new Shared.MediaPlayer(libVLC);

this.SizeChanged += OnSizeChanged;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be unregistered in the Dispose method.


controlHeight = this.Height;
controlWidth = this.Width;

this.Loaded += VideoView_Loaded;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be unregistered in the Dispose method.

}

private void VideoView_Loaded(object sender, RoutedEventArgs e)
{
mediaPlayer.Hwnd = ((System.Windows.Forms.Panel)this.Template.FindName("PART_PlayerView", this)).Handle;

if (this.Content != null)
{
object content = Content;
Content = null;
foreground.Content = (UIElement)content;
}
}

private void VLCResize()
{
uint h = 0, w = 0;
float scale = 1, scalew = 1, scaleh = 1;

if (mediaPlayer.Size(0, ref w, ref h))
{
scalew = (float)controlWidth / (float)w;
w = (uint)controlWidth;

scaleh = (float)controlHeight / (float)h;
h = (uint)controlHeight;

scale = scalew < scaleh ? scalew : scaleh;
mediaPlayer.Scale = scale;
}
}

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
controlWidth = e.NewSize.Width;
controlHeight = e.NewSize.Height;

if (mediaPlayer.IsPlaying)
{
VLCResize();
}
}

public void Dispose()
{
if (mediaPlayer.IsPlaying)
mediaPlayer.Stop();

mediaPlayer.Dispose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may add _mediaPlayer.Hwnd = IntPtr.Zero

libVLC.Dispose();
}

public LibVLCSharp.Shared.MediaPlayer MediaPlayer { get => mediaPlayer; }
public LibVLCSharp.Shared.LibVLC LibVLC { get => libVLC; }
}
}
71 changes: 71 additions & 0 deletions LibVLCSharp.Wpf/LibVLCSharp.Wpf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DA7A2677-0944-481F-A59B-9128FC54FD5F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LibVLCSharp.WPF</RootNamespace>
<AssemblyName>LibVLCSharp.WPF</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="ForegroundWindow.xaml.cs">
<DependentUpon>ForegroundWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VideoView.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibVLCSharp\LibVLCSharp.csproj">
<Project>{d1c3b7c4-713b-46b2-b33a-e9298c819921}</Project>
<Name>LibVLCSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="ForegroundWindow.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Styles\VideoView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions LibVLCSharp.Wpf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("LibVLCSharp.WPF")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LibVLCSharp.WPF")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("da7a2677-0944-481f-a59b-9128fc54fd5f")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading