看看下面这个视频播放器的代码,我想实现在播放区域,左键单击时播放/暂停切换, 左键双击时全屏播放,
怎么实现?请高手帮忙啊!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX.AudioVideoPlayback;
namespace 用DriectX实现媒体播放
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private Video MyVideo = null ;
public Form1()
{
//
// 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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.FileName = "avi";
this.openFileDialog1.Filter = "视频文件(*.avi;*.rmvb)|*.avi;*.rmvb|所有文件(*.*)|*.*";
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(-24, -16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(488, 280);
this.panel1.TabIndex = 0;
this.panel1.Click += new System.EventHandler(this.panel1_DoubleClick);
this.panel1.DoubleClick += new System.EventHandler(this.panel1_DoubleClick);
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 264);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "打开";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 264);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "播放";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(216, 264);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "暂停";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(320, 264);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "停止";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 291);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "媒体播放器";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = Application.StartupPath ;
if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK )
{
// 记录panel组件的大小
int height = panel1.Height ;
int width = panel1.Width ;
// 如果存在打开的Video文件,释放它
if ( MyVideo != null )
{
MyVideo.Dispose ( ) ;
}
// 打开一个新的Video文件
MyVideo = new Video ( openFileDialog1.FileName ) ;
// 把Video文件分配给创建的Panel组件
MyVideo.Owner = panel1 ;
// 以记录的panel组件的大小来重新定义
panel1.Width = width ;
panel1.Height = height ;
// 播放AVI文件的第一帧,主要是为了在panel中显示
MyVideo.Play ( ) ;
MyVideo.Pause ( ) ;
}
//确定窗体中的各按钮状态
if ( MyVideo == null )
{
button2.Enabled = false ;
button3.Enabled = false ;
button4.Enabled = false ;
}
else
{
button2.Enabled = true ;
button3.Enabled = true ;
button4.Enabled = true ;
}
}
private void button2_Click ( object sender, System.EventArgs e )
{
if ( MyVideo != null )
{
MyVideo.Play ( ) ;
}
}
private void button3_Click ( object sender, System.EventArgs e )
{
if ( MyVideo != null )
{
MyVideo.Pause ( ) ;
}
}
private void button4_Click ( object sender, System.EventArgs e )
{
if ( MyVideo != null )
{
MyVideo.Stop ( ) ;
}
}
//初始化窗体中各按钮的状态
private void Form1_Load ( object sender, System.EventArgs e )
{
if ( MyVideo == null )
{
button2.Enabled = false ;
button3.Enabled = false ;
button4.Enabled = false ;
}
else
{
button2.Enabled = true ;
button3.Enabled = true ;
button4.Enabled = true ;
}
}
public void FullScreenPlay()
{
if(MyVideo.Fullscreen==false)
MyVideo.Fullscreen=true;
else
MyVideo.Fullscreen=false;
}
public int count=0;
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)//左键双击全屏
{
count++;
if(count==2)
{
count=0;
FullScreenPlay();
}
}
if(e.Button==MouseButtons.Right)//右键单击播放/暂停
{
if ( MyVideo != null )
{
if(MyVideo.Playing == true)
MyVideo.Pause();
else
if(MyVideo.Paused == true)
MyVideo.Play ();
}
}
}
private void panel1_DoubleClick(object sender, System.EventArgs e)
{
FullScreenPlay();
MessageBox.Show("sdfsfgdfgfdgdfsfsfsssssssf");
}
}
}