using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Reflection;

namespace Sample09_TransparentControl
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.Button ButtonOrder;
		internal System.Windows.Forms.Button ButtonAbout;
		internal System.Windows.Forms.OpenFileDialog openFileDialogFlashFile;
		internal System.Windows.Forms.Button ButtonPlayFlashFileFromFile;
		private f_in_box__lib.f_in_box__control f_in_box__control1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.PictureBox pictureBox1;
        private f_in_box__lib.f_in_box__control f_in_box__control2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.ButtonOrder = new System.Windows.Forms.Button();
            this.ButtonAbout = new System.Windows.Forms.Button();
            this.ButtonPlayFlashFileFromFile = new System.Windows.Forms.Button();
            this.openFileDialogFlashFile = new System.Windows.Forms.OpenFileDialog();
            this.f_in_box__control1 = new f_in_box__lib.f_in_box__control();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.f_in_box__control2 = new f_in_box__lib.f_in_box__control();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // ButtonOrder
            // 
            this.ButtonOrder.Location = new System.Drawing.Point(488, 448);
            this.ButtonOrder.Name = "ButtonOrder";
            this.ButtonOrder.Size = new System.Drawing.Size(136, 23);
            this.ButtonOrder.TabIndex = 13;
            this.ButtonOrder.Text = "Buy license";
            this.ButtonOrder.Click += new System.EventHandler(this.ButtonOrder_Click);
            // 
            // ButtonAbout
            // 
            this.ButtonAbout.Location = new System.Drawing.Point(344, 448);
            this.ButtonAbout.Name = "ButtonAbout";
            this.ButtonAbout.Size = new System.Drawing.Size(136, 23);
            this.ButtonAbout.TabIndex = 12;
            this.ButtonAbout.Text = "More about F-IN-BOX";
            this.ButtonAbout.Click += new System.EventHandler(this.ButtonAbout_Click);
            // 
            // ButtonPlayFlashFileFromFile
            // 
            this.ButtonPlayFlashFileFromFile.Location = new System.Drawing.Point(8, 448);
            this.ButtonPlayFlashFileFromFile.Name = "ButtonPlayFlashFileFromFile";
            this.ButtonPlayFlashFileFromFile.Size = new System.Drawing.Size(152, 23);
            this.ButtonPlayFlashFileFromFile.TabIndex = 10;
            this.ButtonPlayFlashFileFromFile.Text = "Play SWF / FLV from file...";
            this.ButtonPlayFlashFileFromFile.Click += new System.EventHandler(this.ButtonPlayFlashFileFromFile_Click);
            // 
            // openFileDialogFlashFile
            // 
            this.openFileDialogFlashFile.Filter = "Flash files (*.swf;*.flv)|*.swf;*.flv|Flash movies (*.swf)|*.swf|Flash video file" +
                "s (*.flv)|*.flv|All files (*.*)|*.*||";
            this.openFileDialogFlashFile.RestoreDirectory = true;
            this.openFileDialogFlashFile.ShowReadOnly = true;
            // 
            // f_in_box__control1
            // 
            this.f_in_box__control1.Context = null;
            this.f_in_box__control1.FlashProperty_AlignMode = 0;
            this.f_in_box__control1.FlashProperty_AllowFullscreen = false;
            this.f_in_box__control1.FlashProperty_AllowScriptAccess = "";
            this.f_in_box__control1.FlashProperty_BackgroundColor = -1;
            this.f_in_box__control1.FlashProperty_Base = "";
            this.f_in_box__control1.FlashProperty_BGColor = "";
            this.f_in_box__control1.FlashProperty_DeviceFont = false;
            this.f_in_box__control1.FlashProperty_EmbedMovie = false;
            this.f_in_box__control1.FlashProperty_FlashVars = "";
            this.f_in_box__control1.FlashProperty_FrameNum = -1;
            this.f_in_box__control1.FlashProperty_Loop = true;
            this.f_in_box__control1.FlashProperty_Menu = true;
            this.f_in_box__control1.FlashProperty_Movie = "";
            this.f_in_box__control1.FlashProperty_MovieData = "";
            this.f_in_box__control1.FlashProperty_Playing = true;
            this.f_in_box__control1.FlashProperty_Quality = 1;
            this.f_in_box__control1.FlashProperty_Quality2 = "High";
            this.f_in_box__control1.FlashProperty_SAlign = "";
            this.f_in_box__control1.FlashProperty_Scale = "ShowAll";
            this.f_in_box__control1.FlashProperty_ScaleMode = 0;
            this.f_in_box__control1.FlashProperty_Stacking = "";
            this.f_in_box__control1.FlashProperty_SWRemote = "";
            this.f_in_box__control1.FlashProperty_WMode = "Transparent";
            this.f_in_box__control1.Location = new System.Drawing.Point(48, 112);
            this.f_in_box__control1.Name = "f_in_box__control1";
            this.f_in_box__control1.Size = new System.Drawing.Size(216, 176);
            this.f_in_box__control1.StandardMenu = false;
            this.f_in_box__control1.TabIndex = 18;
            this.f_in_box__control1.Text = "f_in_box__control1";
            this.f_in_box__control1.TransparentMode = true;
            this.f_in_box__control1.UseFlashCursor = true;
            this.f_in_box__control1.OnPaintStage += new f_in_box__lib.f_in_box__control.OnPaintStageEventHandler(this.f_in_box__control_OnPaintStage);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(616, 16);
            this.label1.TabIndex = 19;
            this.label1.Text = "This example demonstrates how to use transparent mode.";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 32);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(616, 16);
            this.label2.TabIndex = 20;
            this.label2.Text = "1. Set TransparentMode = true to enable transparent mode.";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 56);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(616, 40);
            this.label3.TabIndex = 21;
            this.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.";
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(128, 192);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(248, 168);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 22;
            this.pictureBox1.TabStop = false;
            // 
            // f_in_box__control2
            // 
            this.f_in_box__control2.Context = null;
            this.f_in_box__control2.FlashProperty_AlignMode = 0;
            this.f_in_box__control2.FlashProperty_AllowFullscreen = false;
            this.f_in_box__control2.FlashProperty_AllowScriptAccess = "";
            this.f_in_box__control2.FlashProperty_BackgroundColor = -1;
            this.f_in_box__control2.FlashProperty_Base = "";
            this.f_in_box__control2.FlashProperty_BGColor = "";
            this.f_in_box__control2.FlashProperty_DeviceFont = false;
            this.f_in_box__control2.FlashProperty_EmbedMovie = false;
            this.f_in_box__control2.FlashProperty_FlashVars = "";
            this.f_in_box__control2.FlashProperty_FrameNum = -1;
            this.f_in_box__control2.FlashProperty_Loop = true;
            this.f_in_box__control2.FlashProperty_Menu = true;
            this.f_in_box__control2.FlashProperty_Movie = "";
            this.f_in_box__control2.FlashProperty_MovieData = "";
            this.f_in_box__control2.FlashProperty_Playing = true;
            this.f_in_box__control2.FlashProperty_Quality = 1;
            this.f_in_box__control2.FlashProperty_Quality2 = "High";
            this.f_in_box__control2.FlashProperty_SAlign = "";
            this.f_in_box__control2.FlashProperty_Scale = "ShowAll";
            this.f_in_box__control2.FlashProperty_ScaleMode = 0;
            this.f_in_box__control2.FlashProperty_Stacking = "";
            this.f_in_box__control2.FlashProperty_SWRemote = "";
            this.f_in_box__control2.FlashProperty_WMode = "Transparent";
            this.f_in_box__control2.Location = new System.Drawing.Point(280, 264);
            this.f_in_box__control2.Name = "f_in_box__control2";
            this.f_in_box__control2.Size = new System.Drawing.Size(232, 168);
            this.f_in_box__control2.StandardMenu = false;
            this.f_in_box__control2.TabIndex = 23;
            this.f_in_box__control2.Text = "f_in_box__control2";
            this.f_in_box__control2.TransparentMode = true;
            this.f_in_box__control2.UseFlashCursor = true;
            this.f_in_box__control2.OnPaintStage += new f_in_box__lib.f_in_box__control.OnPaintStageEventHandler(this.f_in_box__control_OnPaintStage);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(632, 486);
            this.Controls.Add(this.f_in_box__control2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.f_in_box__control1);
            this.Controls.Add(this.ButtonPlayFlashFileFromFile);
            this.Controls.Add(this.ButtonOrder);
            this.Controls.Add(this.ButtonAbout);
            this.Controls.Add(this.pictureBox1);
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.Text = "Sample 9 -Transparent Control";
            this.Load += new System.EventHandler(this.MainForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			openFileDialogFlashFile.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

	        f_in_box__control1.PutMovieFromStream(this.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Embedded_Movies.movie.swf"));
		    f_in_box__control1.FlashMethod_Play();

			f_in_box__control2.PutMovieFromStream(this.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Embedded_Movies.Transparency.swf"));
			f_in_box__control2.FlashMethod_Play();

			pictureBox1.Image = new System.Drawing.Bitmap(this.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Image.jpg"));
		}

		private void ButtonPlayFlashFileFromFile_Click(object sender, System.EventArgs e)
		{
			if (openFileDialogFlashFile.ShowDialog() == DialogResult.OK)
			{
				String FlashFilePath = openFileDialogFlashFile.FileName;

				if (FlashFilePath.EndsWith(".swf"))
					f_in_box__control1.FlashProperty_Movie = FlashFilePath;
				else
				{
					f_in_box__control1.FlashProperty_FlashVars = "FLVPath=" + System.Web.HttpUtility.UrlEncode(FlashFilePath);
					f_in_box__control1.PutMovieFromStream(this.GetType().Assembly.GetManifestResourceStream("Sample09_TransparentControl.Embedded_Movies.FLVPlayer.swf"));
				}

				f_in_box__control1.FlashMethod_Play();
			}
		}

		private void ButtonAbout_Click(object sender, System.EventArgs e)
		{
			System.Diagnostics.Process.Start("http://www.f-in-box.com/dotnet/");
		}

		private void ButtonOrder_Click(object sender, System.EventArgs e)
		{
			System.Diagnostics.Process.Start("http://www.f-in-box.com/dotnet/order.html");
		}

		private void f_in_box__control_OnPaintStage(object sender, f_in_box__lib.PaintStage stage, System.Drawing.Graphics Canvas)
		{
		
			if (f_in_box__lib.PaintStage.PrePaint == stage)
			{
				f_in_box__lib.f_in_box__control f_in_box__control = (f_in_box__lib.f_in_box__control)sender;

				using (Bitmap b = new Bitmap(Width, Height))
				{
					using (Graphics g = Graphics.FromImage(b))
					{
						PaintEventArgs pea = new PaintEventArgs(g, new Rectangle(f_in_box__control.Location, f_in_box__control.Size));

						this.OnPaintBackground(pea);
						this.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);
					}
				}
			}
		}
	}

	class FromStreamToStreamWriter
	{
		internal System.IO.Stream FromStream, ToStream;
		internal System.Threading.Thread Thread;

		public FromStreamToStreamWriter(System.IO.Stream FromStream, System.IO.Stream ToStream)
		{
			this.FromStream = FromStream;
			this.ToStream = ToStream;

			Thread = new System.Threading.Thread(new System.Threading.ThreadStart(Main));

			Thread.Start();
		}

		private void Main()
		{
			const int nSize = 64 * 1024;
			byte[] buffer = new byte[nSize];

			int nReadBytes;

			while (true)
			{
				nReadBytes = FromStream.Read(buffer, 0, nSize);

				if (0 == nReadBytes)
					break;

				try
				{
					ToStream.Write(buffer, 0, nReadBytes);
				}
				catch (System.IO.IOException /* e */ )
				{
					// Loading is interrupted
					break;
				}
			}

			FromStream.Close();
			ToStream.Close();
		}
	}

	class ContentProvider
	{
		private String Path;
		private f_in_box__lib.AxCode AxCode;
		private f_in_box__lib.AxCode.OnLoadExternalResourceByFullPathEventHandler Handler;

        public ContentProvider(f_in_box__lib.AxCode AxCode, String Path)
		{
			this.Path = Path;
			this.AxCode = AxCode;

			Handler = new f_in_box__lib.AxCode.OnLoadExternalResourceByFullPathEventHandler(OnLoadExternalResourceByFullPath);

			AxCode.OnLoadExternalResourceByFullPath += Handler;
		}

		private void OnLoadExternalResourceByFullPath(object sender, String URL, System.IO.Stream Stream, ref bool Handled)
		{
			if (URL == "http://FLV/FlashVideo.flv")
			{
				System.IO.Stream FromStream = System.IO.File.OpenRead(Path);

				FromStreamToStreamWriter writer = 
					new FromStreamToStreamWriter(
						FromStream, 
						Stream
					);

				Handled = true;

				AxCode.OnLoadExternalResourceByFullPath -= Handler;
			}
		}
	}

}
