Question

How can I go about hosting flash content inside a WPF form and still use transparency/alpha on my WPF window? Hosting a WinForms flash controls does not allow this.

Was it helpful?

Solution

Unless the control you use to display the Flash content is built in WPF, you will run in to these "airspace" issues. Every display technology from Win32 to WinForms used HWNDs "under the hood", but WPF uses DirectX. The Window Manager in Windows however, still only understands HWNDs, so WPF apps have one top-level HWND-based window, and everything under that is done in DirectX (actually things like context menus and tooltips also have top-level HWNDs as well). Adam Nathan has a very good description of WPF interop in this article.

OTHER TIPS

Although I haven't done it, you can probably use the WebBrowser control found in WPF 3.5 sp1 to wrap your Flash content within WPF. I'm not sure how the transparency will be affected though.

Can you use Expression to convert the flash content to XAML? I believe that there are tools in there or off to the side that do this.

Just have been struggling with same problem of how to upload & Make WPF transparent with ability of displaying Flash, because if you enable on your MainWindow "Allow transparency" Flash will not show once the application will run.

1) I used WebBrowser Control to play Flash(.swf) files. They are on my PC, however it can play from internet or wherever you have hosted them. Don't forget to name your WebBrowser Control to get to it in C#.

 private void Window_Loaded(object sender, RoutedEventArgs e)
 {   
    MyHelper.ExtendFrame(this, new Thickness(-1));    
    this.MyBrowser.Navigate(@"C:\Happy\Download\flash\PlayWithMEGame.swf");         
 }

2) Now for transparency. I have set in WPF 'false' to "Allow Transparency" and set "Window Style" to 'None'. After that I have used information from HERE and HERE and created a following code that produced desired effect of allowing transparency on MainWindow and running Flash at same time, here is my code:

public class MyHelper
{
    public static bool ExtendFrame(Window window, Thickness margin)
    {
        IntPtr hwnd = new WindowInteropHelper(window).Handle;
        window.Background = Brushes.Transparent;
        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
        MARGINS margins = new MARGINS(margin);
        DwmExtendFrameIntoClientArea(hwnd, ref margins);
        return true;
    }
    [DllImport("dwmapi.dll", PreserveSig = false)]
    static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
}

 struct MARGINS
    {
        public MARGINS(Thickness t)
        {
            Left = (int)t.Left;
            Right = (int)t.Right;
            Top = (int)t.Top;
            Bottom = (int)t.Bottom;
        }
        public int Left;
        public int Right;
        public int Top;
        public int Bottom;
    }

And called it from Window_Loaded() + you need 'below' line for 'DllImport' to work.

using System.Runtime.InteropServices;
using System.Windows.Interop;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top