Imports System.Drawing

Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents openFileDialogFlashFile As System.Windows.Forms.OpenFileDialog
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents f_in_box__control1 As f_in_box__lib.f_in_box__control
    Friend WithEvents ButtonPlayFlashFileFromFile As System.Windows.Forms.Button
    Friend WithEvents ButtonOrder As System.Windows.Forms.Button
    Friend WithEvents ButtonAbout As System.Windows.Forms.Button
    Friend WithEvents pictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents F_in_box__control2 As f_in_box__lib.f_in_box__control
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.label3 = New System.Windows.Forms.Label
        Me.openFileDialogFlashFile = New System.Windows.Forms.OpenFileDialog
        Me.label2 = New System.Windows.Forms.Label
        Me.label1 = New System.Windows.Forms.Label
        Me.f_in_box__control1 = New f_in_box__lib.f_in_box__control
        Me.ButtonPlayFlashFileFromFile = New System.Windows.Forms.Button
        Me.ButtonOrder = New System.Windows.Forms.Button
        Me.ButtonAbout = New System.Windows.Forms.Button
        Me.pictureBox1 = New System.Windows.Forms.PictureBox
        Me.F_in_box__control2 = New f_in_box__lib.f_in_box__control
        Me.SuspendLayout()
        '
        'label3
        '
        Me.label3.Location = New System.Drawing.Point(8, 60)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(616, 40)
        Me.label3.TabIndex = 29
        Me.label3.Text = "2. You can provide a background to simulate transparency. Use event OnPaintStage." & _
        " This example shows how to use this event. See source code."
        '
        'openFileDialogFlashFile
        '
        Me.openFileDialogFlashFile.Filter = "Flash files (*.swf;*.flv)|*.swf;*.flv|Flash movies (*.swf)|*.swf|Flash video file" & _
        "s (*.flv)|*.flv|All files (*.*)|*.*||"
        Me.openFileDialogFlashFile.RestoreDirectory = True
        Me.openFileDialogFlashFile.ShowReadOnly = True
        '
        'label2
        '
        Me.label2.Location = New System.Drawing.Point(8, 36)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(616, 16)
        Me.label2.TabIndex = 28
        Me.label2.Text = "1. Set TransparentMode = true to enable transparent mode."
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(8, 12)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(616, 16)
        Me.label1.TabIndex = 27
        Me.label1.Text = "This example demonstrates how to use transparent mode."
        '
        'f_in_box__control1
        '
        Me.f_in_box__control1.Context = Nothing
        Me.f_in_box__control1.FlashProperty_AlignMode = 0
        Me.f_in_box__control1.FlashProperty_AllowScriptAccess = ""
        Me.f_in_box__control1.FlashProperty_BackgroundColor = -1
        Me.f_in_box__control1.FlashProperty_Base = ""
        Me.f_in_box__control1.FlashProperty_BGColor = ""
        Me.f_in_box__control1.FlashProperty_DeviceFont = False
        Me.f_in_box__control1.FlashProperty_EmbedMovie = False
        Me.f_in_box__control1.FlashProperty_FlashVars = ""
        Me.f_in_box__control1.FlashProperty_FrameNum = -1
        Me.f_in_box__control1.FlashProperty_Loop = True
        Me.f_in_box__control1.FlashProperty_Menu = True
        Me.f_in_box__control1.FlashProperty_Movie = ""
        Me.f_in_box__control1.FlashProperty_MovieData = ""
        Me.f_in_box__control1.FlashProperty_Playing = True
        Me.f_in_box__control1.FlashProperty_Quality = 1
        Me.f_in_box__control1.FlashProperty_Quality2 = "High"
        Me.f_in_box__control1.FlashProperty_SAlign = ""
        Me.f_in_box__control1.FlashProperty_Scale = "ShowAll"
        Me.f_in_box__control1.FlashProperty_ScaleMode = 0
        Me.f_in_box__control1.FlashProperty_Stacking = ""
        Me.f_in_box__control1.FlashProperty_SWRemote = ""
        Me.f_in_box__control1.FlashProperty_WMode = "Transparent"
        Me.f_in_box__control1.Location = New System.Drawing.Point(48, 116)
        Me.f_in_box__control1.Name = "f_in_box__control1"
        Me.f_in_box__control1.Size = New System.Drawing.Size(216, 132)
        Me.f_in_box__control1.StandardMenu = False
        Me.f_in_box__control1.TabIndex = 26
        Me.f_in_box__control1.Text = "f_in_box__control1"
        Me.f_in_box__control1.TransparentMode = True
        '
        'ButtonPlayFlashFileFromFile
        '
        Me.ButtonPlayFlashFileFromFile.Location = New System.Drawing.Point(8, 452)
        Me.ButtonPlayFlashFileFromFile.Name = "ButtonPlayFlashFileFromFile"
        Me.ButtonPlayFlashFileFromFile.Size = New System.Drawing.Size(152, 23)
        Me.ButtonPlayFlashFileFromFile.TabIndex = 23
        Me.ButtonPlayFlashFileFromFile.Text = "Play SWF / FLV from file..."
        '
        'ButtonOrder
        '
        Me.ButtonOrder.Location = New System.Drawing.Point(488, 452)
        Me.ButtonOrder.Name = "ButtonOrder"
        Me.ButtonOrder.Size = New System.Drawing.Size(136, 23)
        Me.ButtonOrder.TabIndex = 25
        Me.ButtonOrder.Text = "Buy license"
        '
        'ButtonAbout
        '
        Me.ButtonAbout.Location = New System.Drawing.Point(344, 452)
        Me.ButtonAbout.Name = "ButtonAbout"
        Me.ButtonAbout.Size = New System.Drawing.Size(136, 23)
        Me.ButtonAbout.TabIndex = 24
        Me.ButtonAbout.Text = "More about F-IN-BOX"
        '
        'pictureBox1
        '
        Me.pictureBox1.Location = New System.Drawing.Point(128, 196)
        Me.pictureBox1.Name = "pictureBox1"
        Me.pictureBox1.Size = New System.Drawing.Size(248, 168)
        Me.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.pictureBox1.TabIndex = 30
        Me.pictureBox1.TabStop = False
        '
        'F_in_box__control2
        '
        Me.F_in_box__control2.Context = Nothing
        Me.F_in_box__control2.FlashProperty_AlignMode = 0
        Me.F_in_box__control2.FlashProperty_AllowScriptAccess = ""
        Me.F_in_box__control2.FlashProperty_BackgroundColor = -1
        Me.F_in_box__control2.FlashProperty_Base = ""
        Me.F_in_box__control2.FlashProperty_BGColor = ""
        Me.F_in_box__control2.FlashProperty_DeviceFont = False
        Me.F_in_box__control2.FlashProperty_EmbedMovie = False
        Me.F_in_box__control2.FlashProperty_FlashVars = ""
        Me.F_in_box__control2.FlashProperty_FrameNum = -1
        Me.F_in_box__control2.FlashProperty_Loop = True
        Me.F_in_box__control2.FlashProperty_Menu = True
        Me.F_in_box__control2.FlashProperty_Movie = ""
        Me.F_in_box__control2.FlashProperty_MovieData = ""
        Me.F_in_box__control2.FlashProperty_Playing = True
        Me.F_in_box__control2.FlashProperty_Quality = 1
        Me.F_in_box__control2.FlashProperty_Quality2 = "High"
        Me.F_in_box__control2.FlashProperty_SAlign = ""
        Me.F_in_box__control2.FlashProperty_Scale = "ShowAll"
        Me.F_in_box__control2.FlashProperty_ScaleMode = 0
        Me.F_in_box__control2.FlashProperty_Stacking = ""
        Me.F_in_box__control2.FlashProperty_SWRemote = ""
        Me.F_in_box__control2.FlashProperty_WMode = "Transparent"
        Me.F_in_box__control2.Location = New System.Drawing.Point(224, 272)
        Me.F_in_box__control2.Name = "F_in_box__control2"
        Me.F_in_box__control2.Size = New System.Drawing.Size(280, 152)
        Me.F_in_box__control2.StandardMenu = False
        Me.F_in_box__control2.TabIndex = 31
        Me.F_in_box__control2.Text = "F_in_box__control2"
        Me.F_in_box__control2.TransparentMode = True
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(632, 486)
        Me.Controls.Add(Me.F_in_box__control2)
        Me.Controls.Add(Me.label2)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.f_in_box__control1)
        Me.Controls.Add(Me.ButtonPlayFlashFileFromFile)
        Me.Controls.Add(Me.ButtonOrder)
        Me.Controls.Add(Me.ButtonAbout)
        Me.Controls.Add(Me.pictureBox1)
        Me.Controls.Add(Me.label3)
        Me.MaximizeBox = False
        Me.Name = "MainForm"
        Me.Text = "Sample 9 - Transparent Control"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        openFileDialogFlashFile.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath())

        f_in_box__control1.PutMovieFromStream(Me.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.movie.swf"))
        f_in_box__control1.FlashMethod_Play()

        F_in_box__control2.PutMovieFromStream(Me.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Transparency.swf"))
        F_in_box__control2.FlashMethod_Play()

        pictureBox1.Image = New System.Drawing.Bitmap(Me.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Image.jpg"))
    End Sub

    Private Sub f_in_box__control1_OnPaintStage(ByVal sender As Object, ByVal stage As f_in_box__lib.PaintStage, ByVal Canvas As System.Drawing.Graphics) Handles f_in_box__control1.OnPaintStage, F_in_box__control2.OnPaintStage
        If f_in_box__lib.PaintStage.PrePaint = stage Then
            Dim f_in_box__control As f_in_box__lib.f_in_box__control = sender

            Dim b As Bitmap = New Bitmap(Width, Height)
            Dim g As Graphics = Graphics.FromImage(b)
            Dim pea As PaintEventArgs = New PaintEventArgs(g, New Rectangle(f_in_box__control.Location, f_in_box__control.Size))

            Me.OnPaintBackground(pea)
            Me.OnPaint(pea)

            g.DrawImage( _
                pictureBox1.Image, _
                New Rectangle(pictureBox1.Location, pictureBox1.Size), _
                New Rectangle(New Point(0, 0), pictureBox1.Image.Size), _
                GraphicsUnit.Pixel)

            Canvas.DrawImage( _
                b, _
                New Rectangle(New Point(0, 0), f_in_box__control.Size), _
                New Rectangle(f_in_box__control.Location, f_in_box__control.Size), _
                GraphicsUnit.Pixel)

            b.Dispose()
            g.Dispose()

        End If
    End Sub

    Private Sub ButtonPlayFlashFileFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlayFlashFileFromFile.Click
        If openFileDialogFlashFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If openFileDialogFlashFile.FileName.EndsWith(".swf") Then
                f_in_box__control1.FlashProperty_Movie = openFileDialogFlashFile.FileName
                f_in_box__control1.FlashMethod_Play()
            Else
                'Dim ContentProvider As New ContentProvider(f_in_box__control1.AxCode, openFileDialogFlashFile.FileName)

                f_in_box__control1.FlashProperty_FlashVars = "FLVPath=file://" + System.Web.HttpUtility.UrlEncode(openFileDialogFlashFile.FileName)
                f_in_box__control1.PutMovieFromStream(Me.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.FLVPlayer.swf"))
                f_in_box__control1.FlashMethod_Play()
            End If
        End If
    End Sub

    Private Sub ButtonAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAbout.Click
        System.Diagnostics.Process.Start("http://www.f-in-box.com/dotnet/")
    End Sub

    Private Sub ButtonOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOrder.Click
        System.Diagnostics.Process.Start("http://www.f-in-box.com/dotnet/order.html")
    End Sub
End Class

Class FromStreamToStreamWriter
    Private FromStream As System.IO.Stream
    Private ToStream As System.IO.Stream
    Private Thread As System.Threading.Thread

    Public Sub New(ByVal FromStream As System.IO.Stream, ByVal ToStream As System.IO.Stream)
        Me.FromStream = FromStream
        Me.ToStream = ToStream

        Thread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Main))

        Thread.Start()
    End Sub

    Private Sub Main()
        Const nSize As Integer = 64 * 1024
        Dim buffer(nSize) As Byte

        Dim nReadBytes As Integer

        While (True)
            nReadBytes = FromStream.Read(buffer, 0, nSize)

            If 0 = nReadBytes Then Exit While

            Try
                ToStream.Write(buffer, 0, nReadBytes)
            Catch e As System.IO.IOException
                ' Loading is interrupted
                Exit While
            End Try

        End While

        FromStream.Close()
        ToStream.Close()
    End Sub
End Class

Class ContentProvider
    Private Path As String
    Private AxCode As f_in_box__lib.AxCode

    Sub New(ByVal AxCode As f_in_box__lib.AxCode, ByVal Path As String)
        Me.Path = Path
        Me.AxCode = AxCode

        AddHandler AxCode.OnLoadExternalResourceByFullPath, AddressOf OnLoadExternalResourceByFullPath
    End Sub

    Private Sub OnLoadExternalResourceByFullPath(ByVal sender As Object, ByVal URL As String, ByVal Stream As System.IO.Stream, ByRef Handled As Boolean)
        If URL = "http://FLV/FlashVideo.flv" Then
            Dim FromStream As System.IO.Stream = System.IO.File.OpenRead(Path)

            Dim writer As FromStreamToStreamWriter = New FromStreamToStreamWriter(FromStream, Stream)

            Handled = True

            RemoveHandler AxCode.OnLoadExternalResourceByFullPath, AddressOf OnLoadExternalResourceByFullPath
        End If
    End Sub
End Class
