基于C#做的FlashPlayer-SourceCode

本文详细介绍了使用C#编程语言实现FlashPlayer的过程,包括关键代码段、数据类型如float和string的处理,以及文件操作和异常处理。通过阅读,读者可以了解到如何在C#环境中构建和理解FlashPlayer的源码。
摘要由CSDN通过智能技术生成

 

// form1.cs
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;
using  System.Threading;


namespace  FlashPlayer
{
    
public partial class Form1 : Form
    
{
        
private enum States { RESET, START, WORKING, COMPLETED};
        
private enum Direction { FIRST, FORWARD, BACKWORD, LAST};
        
private const string m_filename = "swf.dat";        
        
private Form2 m_dsp = new Form2();
        
private const int m_interval = 100;        
        
private bool m_needSave = false;
        
private string m_current_swf = "";       
        
        
public Form1()
        
{            
            InitializeComponent();
            m_dsp.Hide();
            m_dsp.MainForm 
= this;                      
        }

        
private void SwitchtoPlayer()
        
{
            m_dsp.fullScreen();
            m_dsp.Focus();
            
this.Hide();
        }

        
void Form1_KeyDown(object sender, KeyEventArgs e)
        
{
            HandleHotKey(e);
            e.Handled 
= true;           
        }

        
        
public void HandleHotKey(KeyEventArgs e)
        
{            
            
if (e.Modifiers == Keys.Alt)
            
{
                
switch (e.KeyCode)
                
{
                    
case Keys.F4:
                        
this.Close();
                        
break;
                    
case Keys.N:
                        MoveInList(Direction.FORWARD);
                        
break;
                    
case Keys.B:
                        MoveInList(Direction.BACKWORD);
                        
break;
                    
case Keys.L:
                        MoveInList(Direction.LAST);
                        
break;
                    
case Keys.F:
                        MoveInList(Direction.FIRST);
                        
break;
                    
case Keys.S:
                        
if (m_dsp.IsPlaying)
                        
{
                            m_dsp.Stop();
                        }

                        
break;
                    
case Keys.P:
                        play();
                        
break;
                }

            }

            
else
            
{
                
switch (e.KeyCode)
                
{
                    
case Keys.Escape:
                        
if (m_dsp != null && m_dsp.IsPlaying)
                        
{
                            SwitchtoPlayer();
                        }

                        
break;
                    
case Keys.Enter:
                        
if (m_dsp.IsPlaying)
                        
{
                            m_dsp.Stop();
                        }

                        play();
                        
break;
                    
case Keys.Up:
                        MoveInList(Direction.BACKWORD);
                        
break;
                    
case Keys.Down:
                        MoveInList(Direction.FORWARD);
                        
break;
                }

            }

        }

        
private void MoveInList(Direction d)
        
{
            
if (listBox1.Items.Count > 0)
            
{
                
switch(d)
                
{
                    
case Direction .FORWARD:
                    
this.listBox1.SelectedIndex = this.listBox1.SelectedIndex < listBox1.Items.Count - 1 ? this.listBox1.SelectedIndex + 1 : 0;
                    
break;
                    
case Direction.BACKWORD:
                    
this.listBox1.SelectedIndex = this.listBox1.SelectedIndex - 1 < 0 ? this.listBox1.Items.Count - 1 : this.listBox1.SelectedIndex - 1;
                    
break;
                    
case Direction.FIRST:
                    
this.listBox1.SelectedIndex = 0;
                    
break;
                    
case Direction.LAST:
                    
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
                    
break;
                }

            }
    
        }


        
protected override void OnFormClosing(FormClosingEventArgs e)
        
{
            
if (m_dsp != null)
            
{
                m_dsp.Close();
            }

            
if (m_needSave && DialogResult.OK == MessageBox.Show(this"Do you want to save your play list?""Information", MessageBoxButtons.OKCancel))
            
{
                
using (StreamWriter sw = new StreamWriter(m_filename))
                
{
                    
foreach (Object o in this.listBox1.Items)
                    
{
                        sw.WriteLine((
string)o);
                    }

                }

            }

            
base.OnFormClosing(e);
        }

     
        
public void UpdateStatus(int percent)
        
{
            toolStripProgressBar1.Value 
= percent;
            toolStripStatusLabel2.Text 
= String.Format("Frame:{0}%.", percent);
        }

        
public void UpdateStatus(string message)
        
{
            toolStripStatusLabel2.Text 
= message;
        }

      
        
public void next_step()
        
{            
            
if (this.listBox1.SelectedIndex == this.listBox1.Items.Count - 1)
            
{
                
//m_dsp.Close();                
                this.toolStripButton1.Enabled = true;
                
this.toolStripButton2.Enabled = false;                
            }

            
else
            
{                
                changePlaying(
this.listBox1.SelectedIndex + 1);
            }

        }


        
private void Form1_Load(object sender, EventArgs e)
        
{            
            
this.KeyDown += new KeyEventHandler(Form1_KeyDown);            
            
//this.Left = 100;
            
//this.Top = 0;
            if (File.Exists(m_filename))
            
{
                
using (StreamReader sr = new StreamReader(m_filename))
                
{
                    
while (!sr.EndOfStream)
                    
{
                        listBox1.Items.Add(sr.ReadLine());
                    }

                    
this.toolStripStatusLabel1.Text = String.Format("Total:{0} movies;", listBox1.Items.Count);
                    
if (listBox1.Items.Count > 0)
                    
{
                        
this.toolStripButton1.Enabled = true;
                    }

                }

            }

            
else
            
{
                
this.toolStripStatusLabel1.Text = "Total:0 movies.";
            }
            
        }

        
private void play()
        
{
            
if (listBox1.Items.Count > 0)
            
{
                
if (this.listBox1.SelectedIndex == -1)
                
{
                    
this.listBox1.SelectedIndex = 0;
                }

                
this.toolStripStatusLabel2.Text = String.Format("Current:{0}", listBox1.SelectedIndex);                
                m_current_swf 
= (string)this.listBox1.SelectedItem;
                m_dsp.SWFFile 
= m_current_swf;

                m_dsp.Play();                
                m_dsp.Show();

                
this.listBox1.Focus();                                
                
this.toolStripButton2.Enabled = true;                
            }

            
else
            
{
                MessageBox.Show(
"Please add flash movie into this list.");
            }
            
        }

        
private void toolStripButton1_Click(object sender, EventArgs e)
        
{            
            play();
            
this.toolStripButton1.Enabled = false;
        }


        
void listBox1_DoubleClick(object sender, System.EventArgs e)
        
{            
            changePlaying(listBox1.SelectedIndex);
        }



        
private void toolStripButton2_Click(object sender, EventArgs e)
        
{
            m_dsp.Stop();
            
this.toolStripButton2.Enabled = false;            
            
this.toolStripButton1.Enabled = true;
        }


        
private void toolStripButton4_Click(object sender, EventArgs e)
        
{
            
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            
{
                fbd.Description 
= "Select the folder which contains flash movies.";
                fbd.ShowNewFolderButton 
= false;
                fbd.RootFolder 
= Environment.SpecialFolder.Desktop;
                
if (DialogResult.OK == fbd.ShowDialog())
                
{
                    get_file(fbd.SelectedPath);
                    get_dir(fbd.SelectedPath);                    
                    
this.toolStripStatusLabel1.Text = String.Format("Total:{0} movies;", listBox1.Items.Count);
                    m_needSave 
= true;
                    
if (this.listBox1.Items.Count > 0)
                    
{
                        
this.toolStripButton1.Enabled = true;
                    }

                }

            }

        }

        
private void get_file(string parent)
        
{
            DirectoryInfo di 
= new DirectoryInfo(parent);
            
foreach (FileInfo fi in di.GetFiles())
            
{
                
                
if (fi.Extension == ".swf" 
                    
|| fi.Extension == ".SWF")
                
{
                    
this.listBox1.Items.Add(fi.FullName);
                }

            }

        }

        
private void get_dir(string parent)
        
{            
            DirectoryInfo di 
= new DirectoryInfo(parent);
            
foreach (DirectoryInfo subdir in di.GetDirectories())
            
{                               
                get_file(subdir.FullName);
                get_dir(subdir.FullName);
            }

        }



        
private void toolStripButton3_Click(object sender, EventArgs e)
        
{
            SwitchtoPlayer();
        }


        
private void toolStripButton5_Click(object sender, EventArgs e)
        
{
            
if (this.listBox1.SelectedIndex == -1)
            
{
                MessageBox.Show(
"Please select one movie to be removed.");
            }

            
else
            
{
                
if (m_dsp.IsPlaying)
                
{
                    m_dsp.Stop();                    
                    
this.toolStripButton2.Enabled = false;                    
                    
this.toolStripButton1.Enabled = true;
                }

                
if (listBox1.Items.Count > 1)
                
{
                    
int pos = this.listBox1.SelectedIndex - 1 < 0 ? 0 : this.listBox1.SelectedIndex - 1;
                    
this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
                    
this.listBox1.SelectedIndex = pos;
                    changePlaying(pos);                    
                }

                
else
                
{
                    
this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
                }

            }

        }
        

        
private void toolStripButton7_Click(object sender, EventArgs e)
        
{
            MoveInList(Direction.BACKWORD);
            changePlaying(
this.listBox1.SelectedIndex);            
        }


        
private void toolStripButton9_Click(object sender, EventArgs e)
        
{            
            changePlaying(
0);
        }


        
private void toolStripButton8_Click(object sender, EventArgs e)
        
{
            MoveInList(Direction.FORWARD);
            changePlaying(
this.listBox1.SelectedIndex);
        }


        
private void toolStripButton10_Click(object sender, EventArgs e)
        
{
            changePlaying(
this.listBox1.Items.Count - 1);            
        }


        
private void changePlaying(int selected)
        
{
            
this.listBox1.SelectedIndex = selected;         
            
if(m_dsp.IsPlaying)
            
{
                m_dsp.Stop();
            }
   
            play();
        }
        

        
private void toolStripButton12_Click(object sender, EventArgs e)
        
{
            AboutBox1 abt 
= new AboutBox1();           
            abt.ShowDialog(
this);            
        }


        
private void toolStripButton6_Click(object sender, EventArgs e)
        
{
            
using (StreamWriter sw = new StreamWriter(m_filename))
            
{
                
foreach (Object o in this.listBox1.Items)
                
{
                    sw.WriteLine((
string)o);
                }

            }

           
            m_needSave 
= false;
           
        }


        
private void Form1_SizeChanged(object sender, EventArgs e)
        
{
            
if (this.WindowState == FormWindowState.Normal)
            
{
                
//this.m_dsp.WindowState = FormWindowState.Normal;                
                this.m_dsp.Show();
            }

            
else if (this.WindowState == FormWindowState.Minimized)
            
{
                
//this.m_dsp.WindowState = FormWindowState.Minimized;                
                this.m_dsp.Hide();
            }
            
        }
      

        
private void toolStripButton13_Click(object sender, EventArgs e)
        
{
            
this.listBox1.ClearSelected();
            
this.listBox1.Items.Clear();
            
this.toolStripButton1.Enabled = false;
            
this.toolStripStatusLabel1.Text = "Total:0 movies.";
        }

    }

}



// form2.cs
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Threading;

namespace  FlashPlayer
{
    
public partial class Form2 : Form
    
{
        
private BackgroundWorker m_bgWorker = new BackgroundWorker();                
        
private const int m_interval = 10;
        
private int m_perOld = -1;
        
        
public Form2()
        
{
            InitializeComponent();           
            
            
this.KeyDown += new KeyEventHandler(Form2_KeyDown);
            
this.Shown += new EventHandler(Form2_Shown);            
            m_bgWorker.WorkerSupportsCancellation 
= true;
            m_bgWorker.WorkerReportsProgress 
= true;
            m_bgWorker.DoWork 
+= new DoWorkEventHandler(m_bgWorker_DoWork);
            m_bgWorker.RunWorkerCompleted 
+= new RunWorkerCompletedEventHandler(m_bgWorker_RunWorkerCompleted);
            m_bgWorker.ProgressChanged 
+= new ProgressChangedEventHandler(m_bgWorker_ProgressChanged);
        }


        
void m_bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        
{
            
if (e.Cancelled)
            
{
                m_main_form.UpdateStatus(
"Cancelled.");
            }

            
else if (e.Error != null)
            
{
                m_main_form.UpdateStatus(e.Error.Message);
            }

            
else
            
{
                m_main_form.UpdateStatus(
"Finished.");
                m_main_form.next_step();
            }

        }


        
void m_bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        
{
            m_main_form.UpdateStatus(e.ProgressPercentage);
            
//lock (m_bgWorker)
            
//{                 
            
//    m_main_form.UpdateStatus(e.ProgressPercentage);
            
//}
        }


        
void percent(BackgroundWorker worker, DoWorkEventArgs e)
        
{            
            
for (int i = 0; i < Int32.MaxValue; i++)
            
{
                
if (worker.CancellationPending && !e.Cancel)
                
{
                    e.Cancel 
= true;
                    
return;
                }

                
else
                
{
                    
int cf = axShockwaveFlash1.CurrentFrame() + 1;
                    
int tf = axShockwaveFlash1.TotalFrames;
                    
int perNow = (int)(100 * ((float)cf / (float)tf));

                    
if (m_perOld != perNow)
                    
{
                        worker.ReportProgress(perNow);
                        m_perOld 
= perNow;
                    }

                    
else
                    
{
                        Thread.Sleep(
3000);
                        cf 
= axShockwaveFlash1.CurrentFrame() + 1;
                        tf 
= axShockwaveFlash1.TotalFrames;
                        perNow 
= (int)(100 * ((float)cf / (float)tf));
                        
if (m_perOld == perNow && !axShockwaveFlash1.Playing)
                        
{
                            
//not change
                            
//playing is over
                            
//or need input to start.
                            break;
                        }

                    }

                    
if (perNow == 100)
                    
{
                        
break;
                    }

                }

                Thread.Sleep(m_interval);
            }

        }

        
void m_bgWorker_DoWork(object sender, DoWorkEventArgs e)
        
{
            BackgroundWorker worker 
= sender as BackgroundWorker;
            percent(worker, e);
        }
  
        
void Form2_Shown(object sender, EventArgs e)
        
{
            
this.Left = m_main_form.Left;
            
this.Top = m_main_form.Top + m_main_form.Height;
            
this.ClientSize = new Size(m_main_form.Width, m_main_form.Width * 3 / 4);            
        }

        
void Form2_KeyDown(object sender, KeyEventArgs e)
        
{
            
if (e.KeyCode == Keys.Escape)
            
{
                resize();
            }
            
            
else
            
{
                m_main_form.HandleHotKey(e);
            }

            e.Handled 
= true;                   
        }
       
         
        
private Form1 m_main_form;        
        
public Form1 MainForm
        
{
            
set
            
{
                m_main_form 
= value;
            }

        }

        
public bool IsPlaying
        
{
            
get
            
{
                
return  axShockwaveFlash1.Playing;            
            }

        }

        
        
public void fullScreen()
        
{
            
this.ClientSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            
this.Top = 0;
            
this.Left = 0;
            Cursor.Hide();
        }

        
private void resize()
        
{            
            
this.ClientSize = new Size(m_main_form.Width, m_main_form.Width * 3/4);
            
this.Left = m_main_form.Left;
            
this.Top = m_main_form.Top + m_main_form.Height;
            
this.m_main_form.Show();
            
this.m_main_form.Focus();
            Cursor.Show();
        }

        
//protected override void OnFormClosing(FormClosingEventArgs e)
        
//{
        
//    Stop();
        
//    this.axShockwaveFlash1.Dispose();            
        
//    base.OnFormClosing(e);
        
//}
        
        
public string SWFFile
        
{
            
get
            
{              
                
return this.axShockwaveFlash1.Movie;
            }

            
set
            
{               
                
this.axShockwaveFlash1.Movie = value;                                
                axShockwaveFlash1.Loop 
= false;
            }

        }

        
public int CurrFrame
        
{
            
get
            
{
                
return axShockwaveFlash1.FrameNum;
                
//return axShockwaveFlash1.CurrentFrame();
            }

            
set
            
{
                axShockwaveFlash1.FrameNum 
= value;
                
//axShockwaveFlash1.GotoFrame(value);
            }

        }
                        
        
public void Play()
        
{           
            
this.axShockwaveFlash1.Play();             
            m_bgWorker.RunWorkerAsync();
        }
        
        
public void Stop()
        
{
            axShockwaveFlash1.Stop();
            
while (axShockwaveFlash1.Playing)
            
{
                Application.DoEvents();
            }

            m_bgWorker.CancelAsync();
            
while (m_bgWorker.IsBusy)
            
{
                Application.DoEvents();
            }

        }
        
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值