C#中对ActiveX控件的调用

一、不生成具有强名称的程序集

1.使用下面这两个命令将COM控件转换成.Net程序集
aximp c:/windows/system/shdocvw.dll

tlbimp mshtml.tlb

aximp将产生两个文件:AxSHDocVw.dll and SHDocVw.dll.
tlbimp将产生MSHTML.dll,它包含了3000多个 DHTML DOM的接口,所以转换可能需要一些时间.

2. 上面产生的DLL文件直接可由.Net应用程序调用了。

二、生成具有强名称的程序集

1.用sn 命令生成三个snk文件,分别是后面将要生成的程序集的密钥文件


Sn –k AxSHdoc.snk
Sn –k shdocvw.snk
Sn –k mshtml.snk

2.使用上面aximp 和 tlbimp 生成这三个程序集文件,这些命令加上/keyfile:可选项
aximp c:/windows/system/shdocvw.dll /keyfile:AxSHDOC.snk shdocvw.snk
tlbimp mshtml.tlb /keyfile:mshtml.snk

这样就可以生成具有强名称的程序集了。

3.把它们加到程序集缓存中,分别调用

Gacutil axshdocvw.dll

Gacutil shdocvw.dll

Gacutil mshtml.dll
打开全局程序集缓存,就可以看到这三个程序集已经在列表中了。

在C:/winnt/assembly也可以看到了。

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

播放器如图: 
 

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 );
        }
 
        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
 
        /** <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)
        {
        
        }
    }
}

 

MS.NET2003中缺少对一些动态链接库的引用!
其中的MediaPlayer.dll 和 AxMediaPlayer.dll需要自己生成


下面的命令为媒体播放器控件 msdxm.ocx 生成 MediaPlayer.dll 和 AxMediaPlayer.dll。
aximp c:/winnt/system32/msdxm.ocx

通常msdxm.ocx中的ActiveX控件都未注册!
运行regsvr32 msdxm.ocx手动注册便生成需要的动态连接库文件.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值