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