淺析C#調(diào)用ActiveX控件
C#調(diào)用ActiveX控件編寫播放器時(shí),遇到了不少問題!發(fā)現(xiàn)MS.NET2003中缺少對(duì)一些動(dòng)態(tài)鏈接庫的引用!在C#調(diào)用ActiveX控件等多媒體控件時(shí),需要用到的MediaPlayer.dll 和 AxMediaPlayer.dll需要自己生成。首先用下面的命令為媒體播放器控件:msdxm.ocx 生成 MediaPlayer.dll 和 AxMediaPlayer.dll。
aximp c:\winnt\system32\msdxm.ocx
而通常msdxm.ocx中的ActiveX控件都未注冊(cè)!再運(yùn)行regsvr32 msdxm.ocx手動(dòng)注冊(cè)便生成需要的動(dòng)態(tài)連接庫文件.在項(xiàng)目中添加對(duì)MediaPlayer.dll 和 AxMediaPlayer.dll的引用,并在程序中插入:using MediaPlayer便完成了整個(gè)調(diào)用過程!
C#調(diào)用ActiveX控件源程序代碼如下:
- 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>
- ///必需的設(shè)計(jì)器變量。
- ///</summary>
- privateSystem.ComponentModel.Containercomponents=null;
- publicForm1()
- {
- //
- //Windows窗體設(shè)計(jì)器支持所必需的
- //
- InitializeComponent();
- //
- //TODO:在InitializeComponent調(diào)用后添加任何構(gòu)造函數(shù)代碼
- //
- }
- /**////<summary>
- ///清理所有正在使用的資源。
- ///</summary>
- protectedoverridevoidDispose(booldisposing)
- {
- if(disposing)
- {
- if(components!=null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
【編輯推薦】