Skip to content

Instantly share code, notes, and snippets.

@JoanComasFdz
Created December 28, 2011 10:20
Show Gist options
  • Save JoanComasFdz/1527463 to your computer and use it in GitHub Desktop.
Save JoanComasFdz/1527463 to your computer and use it in GitHub Desktop.
Basic WPF control to contain a SWF Flash object
public partial class WpfFlashControl
{
/// <summary>
/// Allows an WPF ui element to embed a WinForms element.
/// </summary>
private WindowsFormsHost _host;
/// <summary>
/// The WinForms flash control.
/// </summary>
private WinFormsFlashControl _player;
/// <summary>
/// Gets or sets the path to the .swf file.
/// </summary>
public Uri Source
{
[System.Diagnostics.DebuggerStepThrough]
get { return _player.Source; }
set
{
//
// Reset visibilities
//
_host.Visibility = Visibility.Visible;
txt_error.Visibility = Visibility.Collapsed;
//
// Get the path to the file
//
string path = Path.GetFullPath(value.LocalPath);
//
// Check and load
//
if (!string.IsNullOrEmpty(path) && !File.Exists(path))
{
//
// Show errors
//
txt_error.Text = string.Format("File not found:\r\n{0}", path);
txt_error.Visibility = Visibility.Visible;
_host.Visibility = Visibility.Collapsed;
} else {
//
// No errors, load file.
//
_player.Source = value;
}
}
}
public WpfFlashControl()
{
InitializeComponent();
//
// Hooke the SizeChanged event to resize the inner control.
//
SizeChanged += WpfFlashControlSizeChanged;
//
// Hook the Loaded event to initialize on load, not on
// instantiation.
//
Loaded += WpfFlashControlLoaded;
}
private void WpfFlashControlSizeChanged(object sender, SizeChangedEventArgs e)
{
//
// Check
//
if (_player == null)
return;
//
// Assign size.
//
_player.Width = double.IsNaN(Width) ? (int)ActualWidth : (int)Width;
_player.Height = double.IsNaN(Height) ? (int)ActualHeight : (int)Height;
}
private void WpfFlashControlLoaded(object sender, RoutedEventArgs e)
{
//
// Create the proxy window to win forms
//
_host = new WindowsFormsHost();
//
// Create the winforms control.
//
_player = new WinFormsFlashControl();
//
// Add the winforms control into the proxy.
//
_host.Child = _player;
//
// Add the winforms proxy to the grid.
//
grid_flash.Children.Add(_host);
}
/// <summary>
/// Starts or resumes the file.
/// </summary>
public void Play()
{
_player.Play();
}
/// <summary>
/// Pauses the file.
/// </summary>
public void Pause()
{
_player.Pause();
}
/// <summary>
/// Stops the file.
/// </summary>
public void Stop()
{
_player.Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment