拼吾爱程序人生.Net编程C# 调用ActiveX控件编写播放器

1  /  1  页   1 跳转 查看:561

调用ActiveX控件编写播放器

调用ActiveX控件编写播放器

文/佚名  出处/中国IT实验室

  调用ActiveX控件编写播放器时,遇到了不少问题!

    发现MS.NET2003中缺少对一些动态链接库的引用!
    在调用ActiveX等多媒体控件时,需要用到的MediaPlayer.dll 和 AxMediaPlayer.dll需要自己生成。首先用下面的命令为媒体播放器控件:msdxm.ocx 生成 MediaPlayer.dll 和 AxMediaPlayer.dll。aximp c:\winnt\system32\msdxm.ocx  而通常msdxm.ocx中的ActiveX控件都未注册!
    再运行regsvr32 msdxm.ocx手动注册便生成需要的动态连接库文件.再如图2所示,在项目中添加对MediaPlayer.dll 和 AxMediaPlayer.dll的引用;
    并在程序中插入:using MediaPlayer便完成了整个调用过程!

    播放器如图:
   



    源程序代码如下:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using MediaPlayer;

    namespace AdvancePlayer
    {
        /**//// <summary>
        /// Form1 的摘要说明。
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
        private AxMediaPlayer.AxMediaPlayer axWindowsMediaPlayer1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem menuItemOpen;
        private System.Windows.Forms.MenuItem menuItemClose;
        private System.Windows.Forms.MenuItem menuItemInitSize;
        private System.Windows.Forms.MenuItem menuItemFullScreen;
        private System.Windows.Forms.MenuItem menuItemShowAudioCtrl;
        private System.Windows.Forms.MenuItem menuItemShowPositionCtrl;
        private System.Windows.Forms.MenuItem menuItemShowTrackbarCtrl;
        private System.Windows.Forms.MenuItem menuItemFile;
        private System.Windows.Forms.MenuItem menuItemVideo;
        private System.Windows.Forms.MenuItem menuItemWindow;


        /**//// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /**//// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }




友情提示:此文并不表示本站肯定持有相同观点,转载请注明出处。
 您可能对 [C#] 的这些文章也感兴趣:

BeginnerwithC#1  ADO.NET深入研究(1)
构造函数及参数执行顺序说明  C#实现Web代理服务程序
C#编写的多线程端口扫描程序!  VisualStudio.NET技巧5-10
利用C#重启远程计算机  Microsoft.NET完全介绍(上)
.Net中如何操作IIS(原理篇)  在C#程序设计中使用Win32类库
Visual C#多线程参数传递浅析  使用C#控制远程计算机的服务
使用VisualC#实现断点续传  .Net平台下开发中文语音应用程序
用设计模式固化你的C#程序(下篇)  构造.NET环境下的网页下载器(1)
用C#制作一个目录选择对话框  c#中邮件收发处理(POP3,IMAP,SMTP)
C#锐利体验(6.2)  C#的RawSocket实现网络封包监视
 

回复:调用ActiveX控件编写播放器



Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItemFile = new System.Windows.Forms.MenuItem();
            this.menuItemOpen = new System.Windows.Forms.MenuItem();
            this.menuItemClose = new System.Windows.Forms.MenuItem();
            this.menuItemVideo = new System.Windows.Forms.MenuItem();
            this.menuItemInitSize = new System.Windows.Forms.MenuItem();
            this.menuItemFullScreen = new System.Windows.Forms.MenuItem();
            this.menuItemWindow = new System.Windows.Forms.MenuItem();
            this.menuItemShowAudioCtrl = new System.Windows.Forms.MenuItem();
            this.menuItemShowPositionCtrl = new System.Windows.Forms.MenuItem();
            this.menuItemShowTrackbarCtrl = new System.Windows.Forms.MenuItem();
            this.axWindowsMediaPlayer1 = new AxMediaPlayer.AxMediaPlayer();
            ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).BeginInit();
            this.SuspendLayout();
            //
            // openFileDialog1
            //
            this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
            //
            // mainMenu1
            //
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.menuItemFile,
                                                                                      this.menuItemVideo,
                                                                                      this.menuItemWindow});
            //
            // menuItemFile
            //
            this.menuItemFile.Index = 0;
            this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                        this.menuItemOpen,
                                                                                        this.menuItemClose});
            this.menuItemFile.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
            this.menuItemFile.Text = "文件(&F)";
            //
            // menuItemOpen
            //
            this.menuItemOpen.Index = 0;
            this.menuItemOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
            this.menuItemOpen.Text = "打开(&O)";
            this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
            this.menuItemOpen.Select += new System.EventHandler(this.Form1_Load);
            //
            // menuItemClose
            //
            this.menuItemClose.Index = 1;
            this.menuItemClose.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
            this.menuItemClose.Text = "关闭(&C)";
            this.menuItemClose.Click += new System.EventHandler(this.menuItemClose_Click);
            //
            // menuItemVideo
            //
            this.menuItemVideo.Index = 1;
            this.menuItemVideo.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                          this.menuItemInitSize,
                                                                                          this.menuItemFullScreen});
            this.menuItemVideo.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
            this.menuItemVideo.Text = "视频(&V)";
            //
            // menuItemInitSize
            //
            this.menuItemInitSize.Index = 0;
            this.menuItemInitSize.Shortcut = System.Windows.Forms.Shortcut.CtrlI;
            this.menuItemInitSize.Text = "默认尺寸(&I)";
            this.menuItemInitSize.Click += new System.EventHandler(this.menuItemInitSize_Click);
            //
            // menuItemFullScreen
            //
            this.menuItemFullScreen.Index = 1;
            this.menuItemFullScreen.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
            this.menuItemFullScreen.Text = "全屏(&F)";
            this.menuItemFullScreen.Click += new System.EventHandler(this.menuItemFullScreen_Click);
            //
            // menuItemWindow
            //
            this.menuItemWindow.Index = 2;
            this.menuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                          this.menuItemShowAudioCtrl,
                                                                                          this.menuItemShowPositionCtrl,
                                                                                          this.menuItemShowTrackbarCtrl});
            this.menuItemWindow.Shortcut = System.Windows.Forms.Shortcut.CtrlW;
            this.menuItemWindow.Text = "窗口(&W)";
            //
            // menuItemShowAudioCtrl
            //
            this.menuItemShowAudioCtrl.Checked = true;
            this.menuItemShowAudioCtrl.Index = 0;
            this.menuItemShowAudioCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlU;
            this.menuItemShowAudioCtrl.Text = "音频控制(&U)";
            this.menuItemShowAudioCtrl.Click += new System.EventHandler(this.menuItemShowAudioCtrl_Click);
            //
            // menuItemShowPositionCtrl
            //
            this.menuItemShowPositionCtrl.Checked = true;
            this.menuItemShowPositionCtrl.Index = 1;
            this.menuItemShowPositionCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
            this.menuItemShowPositionCtrl.Text = "播放进度(&P)";
            this.menuItemShowPositionCtrl.Click += new System.EventHandler(this.menuItemShowPositionCtrl_Click);
            //
            // menuItemShowTrackbarCtrl
            //
            this.menuItemShowTrackbarCtrl.Checked = true;
            this.menuItemShowTrackbarCtrl.Index = 2;
            this.menuItemShowTrackbarCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlT;
            this.menuItemShowTrackbarCtrl.Text = "滚动条(&T)";
            this.menuItemShowTrackbarCtrl.Click += new System.EventHandler(this.menuItemShowTrackbarCtrl_Click);
            //
            // axWindowsMediaPlayer1
            //
            this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0, -8);
            this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
            this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
            this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(296, 280);
            this.axWindowsMediaPlayer1.TabIndex = 0;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.BackColor = System.Drawing.SystemColors.Control;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.axWindowsMediaPlayer1);
            this.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.axWindowsMediaPlayer1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


 

回复:调用ActiveX控件编写播放器



/**//// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void menuItemOpen_Click(object sender, System.EventArgs e)
        {
            openFileDialog1.Filter = "视频文件(*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma)|*.avi;*.wmv;*.dat;*.mpg;*.mpeg;*.mov;*.wm;*.wma|音频文件(*.wav;*.mp3;*.snd;*.au;*.midi;*.mid)|*.wav;*.mp3;*.snd;*.au;*.midi;*.mid|所有文件(*.*)|*.*";
            //打开的文件类型
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                axWindowsMediaPlayer1.FileName = openFileDialog1.FileName;
                //如果打开的是音频文件,则禁止【视频】菜单组
                if(openFileDialog1.FilterIndex == 2)
                    menuItemVideo.Enabled = false;
                else
                    menuItemVideo.Enabled = true;
            }

        }

        private void menuItemClose_Click(object sender, System.EventArgs e)
        {
            axWindowsMediaPlayer1.FileName = null;
            menuItemVideo.Enabled = false;
        }

        private void menuItemInitSize_Click(object sender, System.EventArgs e)
        {
            axWindowsMediaPlayer1.DisplaySize = MPDisplaySizeConstants.mpDefaultSize;
        }

        private void menuItemFullScreen_Click(object sender, System.EventArgs e)
        {
            axWindowsMediaPlayer1.DisplaySize = MPDisplaySizeConstants.mpFullScreen;
        }

        private void menuItemShowAudioCtrl_Click(object sender, System.EventArgs e)
        {
            menuItemShowAudioCtrl.Checked = !menuItemShowAudioCtrl.Checked;
            axWindowsMediaPlayer1.ShowTracker = false;
            axWindowsMediaPlayer1.ShowTracker = menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = menuItemShowAudioCtrl.Checked;
        }

        private void menuItemShowPositionCtrl_Click(object sender, System.EventArgs e)
        {
            menuItemShowPositionCtrl.Checked = !menuItemShowPositionCtrl.Checked;
            axWindowsMediaPlayer1.ShowPositionControls = menuItemShowPositionCtrl.Checked;
        }

        private void menuItemShowTrackbarCtrl_Click(object sender, System.EventArgs e)
        {
            menuItemShowTrackbarCtrl.Checked = !menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = false;
            axWindowsMediaPlayer1.ShowTracker = menuItemShowTrackbarCtrl.Checked;
            axWindowsMediaPlayer1.ShowAudioControls = menuItemShowAudioCtrl.Checked;
        }

        private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {

            openFileDialog1.OpenFile();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

          }
        }
    }



 
    如图2中选中的AxMediaPlayer和MediaPlayer既需自己生成的dll,2003系统默认中无迹可寻!他们所起的作用便是实现对ActiceX中播放器相关的函数的调用!
          所以,对于多媒体应用程序的编写,关键在于了解对ActiveX相关控件的调用过程!
 
1  /  1  页   1 跳转

版权所有 拼吾爱程序人生 

Powered by Discuz!NT 2.1.202    Copyright © 2001-2008 Comsenz Inc. 鄂ICP备07500843号
返顶部