C#中对ActiveX控件的调用

本文介绍了在C#中调用ActiveX控件编写播放器时遇到的问题及解决方法。MS.NET2003缺少部分动态链接库引用,需手动为msdxm.ocx生成MediaPlayer.dll和AxMediaPlayer.dll,并注册ActiveX控件。还给出了播放器的源程序代码,强调编写多媒体应用要了解ActiveX控件调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用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便完成了整个调用过程!


播放器如图:
Snap2.jpg


源程序代码如下:
None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
using System.Data;
None.gif
using MediaPlayer;
None.gif
None.gif
namespace AdvancePlayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private AxMediaPlayer.AxMediaPlayer axWindowsMediaPlayer1;
InBlock.gif        
private System.Windows.Forms.OpenFileDialog openFileDialog1;
InBlock.gif        
private System.Windows.Forms.MainMenu mainMenu1;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemOpen;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemClose;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemInitSize;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemFullScreen;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemShowAudioCtrl;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemShowPositionCtrl;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemShowTrackbarCtrl;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemFile;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemVideo;
InBlock.gif        
private System.Windows.Forms.MenuItem menuItemWindow;
InBlock.gif        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(Form1));
InBlock.gif            
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
InBlock.gif            
this.mainMenu1 = new System.Windows.Forms.MainMenu();
InBlock.gif            
this.menuItemFile = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemOpen = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemClose = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemVideo = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemInitSize = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemFullScreen = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemWindow = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemShowAudioCtrl = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemShowPositionCtrl = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.menuItemShowTrackbarCtrl = new System.Windows.Forms.MenuItem();
InBlock.gif            
this.axWindowsMediaPlayer1 = new AxMediaPlayer.AxMediaPlayer();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// openFileDialog1
InBlock.gif            
// 
InBlock.gif
            this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
InBlock.gif            
// 
InBlock.gif            
// mainMenu1
InBlock.gif            
// 
ExpandedSubBlockStart.gifContractedSubBlock.gif
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                      
this.menuItemFile,
InBlock.gif                                                                                      
this.menuItemVideo,
ExpandedSubBlockEnd.gif                                                                                      
this.menuItemWindow}
);
InBlock.gif            
// 
InBlock.gif            
// menuItemFile
InBlock.gif            
// 
InBlock.gif
            this.menuItemFile.Index = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                         
this.menuItemOpen,
ExpandedSubBlockEnd.gif                                                                                         
this.menuItemClose}
);
InBlock.gif            
this.menuItemFile.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
InBlock.gif            
this.menuItemFile.Text = "文件(&F)";
InBlock.gif            
// 
InBlock.gif            
// menuItemOpen
InBlock.gif            
// 
InBlock.gif
            this.menuItemOpen.Index = 0;
InBlock.gif            
this.menuItemOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
InBlock.gif            
this.menuItemOpen.Text = "打开(&O)";
InBlock.gif            
this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
InBlock.gif            
this.menuItemOpen.Select += new System.EventHandler(this.Form1_Load);
InBlock.gif            
// 
InBlock.gif            
// menuItemClose
InBlock.gif            
// 
InBlock.gif
            this.menuItemClose.Index = 1;
InBlock.gif            
this.menuItemClose.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
InBlock.gif            
this.menuItemClose.Text = "关闭(&C)";
InBlock.gif            
this.menuItemClose.Click += new System.EventHandler(this.menuItemClose_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemVideo
InBlock.gif            
// 
InBlock.gif
            this.menuItemVideo.Index = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItemVideo.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                          
this.menuItemInitSize,
ExpandedSubBlockEnd.gif                                                                                          
this.menuItemFullScreen}
);
InBlock.gif            
this.menuItemVideo.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
InBlock.gif            
this.menuItemVideo.Text = "视频(&V)";
InBlock.gif            
// 
InBlock.gif            
// menuItemInitSize
InBlock.gif            
// 
InBlock.gif
            this.menuItemInitSize.Index = 0;
InBlock.gif            
this.menuItemInitSize.Shortcut = System.Windows.Forms.Shortcut.CtrlI;
InBlock.gif            
this.menuItemInitSize.Text = "默认尺寸(&I)";
InBlock.gif            
this.menuItemInitSize.Click += new System.EventHandler(this.menuItemInitSize_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemFullScreen
InBlock.gif            
// 
InBlock.gif
            this.menuItemFullScreen.Index = 1;
InBlock.gif            
this.menuItemFullScreen.Shortcut = System.Windows.Forms.Shortcut.CtrlF;
InBlock.gif            
this.menuItemFullScreen.Text = "全屏(&F)";
InBlock.gif            
this.menuItemFullScreen.Click += new System.EventHandler(this.menuItemFullScreen_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemWindow
InBlock.gif            
// 
InBlock.gif
            this.menuItemWindow.Index = 2;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.menuItemWindow.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] dot.gif{
InBlock.gif                                                                                           
this.menuItemShowAudioCtrl,
InBlock.gif                                                                                           
this.menuItemShowPositionCtrl,
ExpandedSubBlockEnd.gif                                                                                           
this.menuItemShowTrackbarCtrl}
);
InBlock.gif            
this.menuItemWindow.Shortcut = System.Windows.Forms.Shortcut.CtrlW;
InBlock.gif            
this.menuItemWindow.Text = "窗口(&W)";
InBlock.gif            
// 
InBlock.gif            
// menuItemShowAudioCtrl
InBlock.gif            
// 
InBlock.gif
            this.menuItemShowAudioCtrl.Checked = true;
InBlock.gif            
this.menuItemShowAudioCtrl.Index = 0;
InBlock.gif            
this.menuItemShowAudioCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlU;
InBlock.gif            
this.menuItemShowAudioCtrl.Text = "音频控制(&U)";
InBlock.gif            
this.menuItemShowAudioCtrl.Click += new System.EventHandler(this.menuItemShowAudioCtrl_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemShowPositionCtrl
InBlock.gif            
// 
InBlock.gif
            this.menuItemShowPositionCtrl.Checked = true;
InBlock.gif            
this.menuItemShowPositionCtrl.Index = 1;
InBlock.gif            
this.menuItemShowPositionCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
InBlock.gif            
this.menuItemShowPositionCtrl.Text = "播放进度(&P)";
InBlock.gif            
this.menuItemShowPositionCtrl.Click += new System.EventHandler(this.menuItemShowPositionCtrl_Click);
InBlock.gif            
// 
InBlock.gif            
// menuItemShowTrackbarCtrl
InBlock.gif            
// 
InBlock.gif
            this.menuItemShowTrackbarCtrl.Checked = true;
InBlock.gif            
this.menuItemShowTrackbarCtrl.Index = 2;
InBlock.gif            
this.menuItemShowTrackbarCtrl.Shortcut = System.Windows.Forms.Shortcut.CtrlT;
InBlock.gif            
this.menuItemShowTrackbarCtrl.Text = "滚动条(&T)";
InBlock.gif            
this.menuItemShowTrackbarCtrl.Click += new System.EventHandler(this.menuItemShowTrackbarCtrl_Click);
InBlock.gif            
// 
InBlock.gif            
// axWindowsMediaPlayer1
InBlock.gif            
// 
InBlock.gif
            this.axWindowsMediaPlayer1.Location = new System.Drawing.Point(0-8);
InBlock.gif            
this.axWindowsMediaPlayer1.Name = "axWindowsMediaPlayer1";
InBlock.gif            
this.axWindowsMediaPlayer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
InBlock.gif            
this.axWindowsMediaPlayer1.Size = new System.Drawing.Size(296280);
InBlock.gif            
this.axWindowsMediaPlayer1.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.BackColor = System.Drawing.SystemColors.Control;
InBlock.gif            
this.ClientSize = new System.Drawing.Size(292273);
InBlock.gif            
this.Controls.Add(this.axWindowsMediaPlayer1);
InBlock.gif            
this.ForeColor = System.Drawing.SystemColors.ControlText;
InBlock.gif            
this.Menu = this.mainMenu1;
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemOpen_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            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|所有文件(*.*)|*.*";       
InBlock.gif            
//打开的文件类型
InBlock.gif
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                axWindowsMediaPlayer1.FileName 
= openFileDialog1.FileName;
InBlock.gif                
//如果打开的是音频文件,则禁止【视频】菜单组
InBlock.gif
                if(openFileDialog1.FilterIndex == 2)
InBlock.gif                    menuItemVideo.Enabled 
= false;
InBlock.gif                
else
InBlock.gif                    menuItemVideo.Enabled 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemClose_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            axWindowsMediaPlayer1.FileName 
= null;
InBlock.gif            menuItemVideo.Enabled 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemInitSize_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            axWindowsMediaPlayer1.DisplaySize 
= MPDisplaySizeConstants.mpDefaultSize;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void menuItemFullScreen_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            axWindowsMediaPlayer1.DisplaySize 
= MPDisplaySizeConstants.mpFullScreen;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif        
private void menuItemShowPositionCtrl_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            menuItemShowPositionCtrl.Checked 
= !menuItemShowPositionCtrl.Checked;
InBlock.gif            axWindowsMediaPlayer1.ShowPositionControls 
= menuItemShowPositionCtrl.Checked;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif        
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            openFileDialog1.OpenFile();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

Snap1.jpg 
        (图2)
      如图2中选中的AxMediaPlayer和MediaPlayer既需自己生成的dll,2003系统默认中无迹可寻!他们所起的作用便是实现对ActiceX中播放器相关的函数的调用!
      所以,对于多媒体应用程序的编写,关键在于了解对ActiveX相关控件的调用过程!
     
      充分使用.NET中的控件技术,无疑是最直接和最高效的选择,这给我们提供了很大的方便,以往很多需要自己编写的函数和功能,现在都为我们封装成了各种控件,直接调用便可实现!相比往日,效率提高了不少!但其中相关的实现过程和细节还是应该多加了解才好,毕竟,只有让其工作原理烂熟于心,才能将.NET这门强大的技术运用得炉火纯青!

转载于:https://www.cnblogs.com/Advance/archive/2005/03/15/119054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值