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