C#中对ActiveX控件的调用

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

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


播放器如图:



源程序代码如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingMediaPlayer;

namespaceAdvancePlayer
{
/**////<summary>
///Form1的摘要说明。
///</summary>

publicclassForm1:System.Windows.Forms.Form
{
privateAxMediaPlayer.AxMediaPlayeraxWindowsMediaPlayer1;
privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;
privateSystem.Windows.Forms.MainMenumainMenu1;
privateSystem.Windows.Forms.MenuItemmenuItemOpen;
privateSystem.Windows.Forms.MenuItemmenuItemClose;
privateSystem.Windows.Forms.MenuItemmenuItemInitSize;
privateSystem.Windows.Forms.MenuItemmenuItemFullScreen;
privateSystem.Windows.Forms.MenuItemmenuItemShowAudioCtrl;
privateSystem.Windows.Forms.MenuItemmenuItemShowPositionCtrl;
privateSystem.Windows.Forms.MenuItemmenuItemShowTrackbarCtrl;
privateSystem.Windows.Forms.MenuItemmenuItemFile;
privateSystem.Windows.Forms.MenuItemmenuItemVideo;
privateSystem.Windows.Forms.MenuItemmenuItemWindow;


/**////<summary>
///必需的设计器变量。
///</summary>

privateSystem.ComponentModel.Containercomponents=null;

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

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


/**////<summary>
///清理所有正在使用的资源。
///</summary>

protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}

}

base.Dispose(disposing);
}


Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>

privatevoidInitializeComponent()
{
System.Resources.ResourceManagerresources
=newSystem.Resources.ResourceManager(typeof(Form1));
this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
this.mainMenu1=newSystem.Windows.Forms.MainMenu();
this.menuItemFile=newSystem.Windows.Forms.MenuItem();
this.menuItemOpen=newSystem.Windows.Forms.MenuItem();
this.menuItemClose=newSystem.Windows.Forms.MenuItem();
this.menuItemVideo=newSystem.Windows.Forms.MenuItem();
this.menuItemInitSize=newSystem.Windows.Forms.MenuItem();
this.menuItemFullScreen=newSystem.Windows.Forms.MenuItem();
this.menuItemWindow=newSystem.Windows.Forms.MenuItem();
this.menuItemShowAudioCtrl=newSystem.Windows.Forms.MenuItem();
this.menuItemShowPositionCtrl=newSystem.Windows.Forms.MenuItem();
this.menuItemShowTrackbarCtrl=newSystem.Windows.Forms.MenuItem();
this.axWindowsMediaPlayer1=newAxMediaPlayer.AxMediaPlayer();
((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).BeginInit();
this.SuspendLayout();
//
//openFileDialog1
//
this.openFileDialog1.FileOk+=newSystem.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
//mainMenu1
//
this.mainMenu1.MenuItems.AddRange(newSystem.Windows.Forms.MenuItem[]{
this.menuItemFile,
this.menuItemVideo,
this.menuItemWindow}
);
//
//menuItemFile
//
this.menuItemFile.Index=0;
this.menuItemFile.MenuItems.AddRange(newSystem.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+=newSystem.EventHandler(this.menuItemOpen_Click);
this.menuItemOpen.Select+=newSystem.EventHandler(this.Form1_Load);
//
//menuItemClose
//
this.menuItemClose.Index=1;
this.menuItemClose.Shortcut=System.Windows.Forms.Shortcut.CtrlC;
this.menuItemClose.Text="关闭(&C)";
this.menuItemClose.Click+=newSystem.EventHandler(this.menuItemClose_Click);
//
//menuItemVideo
//
this.menuItemVideo.Index=1;
this.menuItemVideo.MenuItems.AddRange(newSystem.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+=newSystem.EventHandler(this.menuItemInitSize_Click);
//
//menuItemFullScreen
//
this.menuItemFullScreen.Index=1;
this.menuItemFullScreen.Shortcut=System.Windows.Forms.Shortcut.CtrlF;
this.menuItemFullScreen.Text="全屏(&F)";
this.menuItemFullScreen.Click+=newSystem.EventHandler(this.menuItemFullScreen_Click);
//
//menuItemWindow
//
this.menuItemWindow.Index=2;
this.menuItemWindow.MenuItems.AddRange(newSystem.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+=newSystem.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+=newSystem.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+=newSystem.EventHandler(this.menuItemShowTrackbarCtrl_Click);
//
//axWindowsMediaPlayer1
//
this.axWindowsMediaPlayer1.Location=newSystem.Drawing.Point(0,-8);
this.axWindowsMediaPlayer1.Name="axWindowsMediaPlayer1";
this.axWindowsMediaPlayer1.OcxState=((System.Windows.Forms.AxHost.State)(resources.GetObject("axWindowsMediaPlayer1.OcxState")));
this.axWindowsMediaPlayer1.Size=newSystem.Drawing.Size(296,280);
this.axWindowsMediaPlayer1.TabIndex=0;
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.BackColor=System.Drawing.SystemColors.Control;
this.ClientSize=newSystem.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+=newSystem.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(
this.axWindowsMediaPlayer1)).EndInit();
this.ResumeLayout(false);

}

#endregion


/**////<summary>
///应用程序的主入口点。
///</summary>

[STAThread]
staticvoidMain()
{
Application.Run(
newForm1());
}


privatevoidmenuItemOpen_Click(objectsender,System.EventArgse)
{
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;
}


}


privatevoidmenuItemClose_Click(objectsender,System.EventArgse)
{
axWindowsMediaPlayer1.FileName
=null;
menuItemVideo.Enabled
=false;
}


privatevoidmenuItemInitSize_Click(objectsender,System.EventArgse)
{
axWindowsMediaPlayer1.DisplaySize
=MPDisplaySizeConstants.mpDefaultSize;
}


privatevoidmenuItemFullScreen_Click(objectsender,System.EventArgse)
{
axWindowsMediaPlayer1.DisplaySize
=MPDisplaySizeConstants.mpFullScreen;
}


privatevoidmenuItemShowAudioCtrl_Click(objectsender,System.EventArgse)
{
menuItemShowAudioCtrl.Checked
=!menuItemShowAudioCtrl.Checked;
axWindowsMediaPlayer1.ShowTracker
=false;
axWindowsMediaPlayer1.ShowTracker
=menuItemShowTrackbarCtrl.Checked;
axWindowsMediaPlayer1.ShowAudioControls
=menuItemShowAudioCtrl.Checked;
}


privatevoidmenuItemShowPositionCtrl_Click(objectsender,System.EventArgse)
{
menuItemShowPositionCtrl.Checked
=!menuItemShowPositionCtrl.Checked;
axWindowsMediaPlayer1.ShowPositionControls
=menuItemShowPositionCtrl.Checked;
}


privatevoidmenuItemShowTrackbarCtrl_Click(objectsender,System.EventArgse)
{
menuItemShowTrackbarCtrl.Checked
=!menuItemShowTrackbarCtrl.Checked;
axWindowsMediaPlayer1.ShowAudioControls
=false;
axWindowsMediaPlayer1.ShowTracker
=menuItemShowTrackbarCtrl.Checked;
axWindowsMediaPlayer1.ShowAudioControls
=menuItemShowAudioCtrl.Checked;
}


privatevoidopenFileDialog1_FileOk(objectsender,System.ComponentModel.CancelEventArgse)
{

openFileDialog1.OpenFile();
}


privatevoidForm1_Load(objectsender,System.EventArgse)
{

}

}

}



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

充分使用.NET中的控件技术,无疑是最直接和最高效的选择,这给我们提供了很大的方便,以往很多需要自己编写的函数和功能,现在都为我们封装成了各种控件,直接调用便可实现!相比往日,效率提高了不少!但其中相关的实现过程和细节还是应该多加了解才好,毕竟,只有让其工作原理烂熟于心,才能将.NET这门强大的技术运用得炉火纯青!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值