`
caozuiba
  • 浏览: 904818 次
文章分类
社区版块
存档分类
最新评论

C#中对ActiveX控件的调用

 
阅读更多
调用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这门强大的技术运用得炉火纯青!
分享到:
评论

相关推荐

    c# activex 控件

    c# activex 控件 html 调用c# 用户控件

    activex控件调用摄像头(能够直接安装使用)

    C#写的activex控件调用摄像头test.html能测试(先在setup2里的debug的msi直接安装)如果需要调用其他摄像头修改html里的document.getElementById('helloworld').start(0)这个0改为其他1或者2看你摄像头被列为几

    c# 编写activex控件 并与网页交互

    c# 编写activex控件 并与网页交互,传递数据给网页,调用网页方法 例子 c# 编写activex控件 并与网页交互,传递数据给网页,调用网页方法 例子

    C++和C#编写Activex控件并进行网页调用

    本资源含有使用C++和C#语言在vs2013开发环境中编写activex控件并使用网页进行调用的程序,并有详细说明,都是工作中的经验结晶,很适合于编写商业软件

    VS2008(c#)开发ActiveX控件、访问及制作CAB包总结

    1.说明如何编写ActiveX控件并制作CAB压缩包。制作一个ActiveX控件,在网页...2.介绍C#如何在ActiveX控件中调用javascript的函数; 3.ActiveX版本更新; 4.测试ActiveX控件在新版操作系统上运行的问题,并提出解决的办法;

    C#开发ActiveX控件

    一个C#开发的ActiveX的例子,有了这个例子,可以根据你的需要随便开发复杂的ActiveX控件。只需把生成的dll放在你的目录下即可。js就能调用,ActiveX也能回调你页面的js。不懂的可以留言。

    使用C#开发ActiveX控件

    最近弄了一个在Qt中使用C#控件的功能,使用的方法是通过C#生产ActiveX控件,在Qt中使用该ActiveX控件。本文这里简单的介绍一下如何用C#生成ActiveX控件。 1. 建立WinForm的UserControl工程,在控件的属性页中选择”...

    C#编写ActiveX控件(纯接口,无界面)

    C#编写ActiveX控件,使用VS2008开发,亲测可用,里面两个方法int Test(int x, int y),string ShowMsg(),并提供IE调用的页面例子。 先用RegAsm.exe对控件进行注册,Cmd命令进入下载解压后的目录(cd命令应该会吧~-~...

    C#调用activeX控件方法步骤[参考].pdf

    C#调用activeX控件方法步骤[参考].pdf

    网页调用海康威视摄像头,C#开发调用海康威视摄像头控件

    本Demo包含海康威视摄像头调用的ActiveX控件制作及调用代码,功能强大(压缩包包含三个文件,一个是ActiveX控件开发项目、一个是控件调用项目、另一个是说明文档)

    C#|activeX手写板控件web调用签名打包自动升级等

    前几天在做查询终端页面由于部分有一定年纪,不会打字的又需要输入姓名等查费用,在本论坛上找到手写输入的,后来就用了然后重新封装activeX控件,给页面调用,整理了一下,C# activeX控件安装包制作成自解压运行cab...

    使用VS2008 C#_开发ActiveX控件

    强烈推荐这个文档,在网上找了好久,很多都不全,初学者对着做根本没法成功。 在现在B/S结果越来越流行的时代,有时候做个网页插件是非常重要的,比如输入控件,打印控件,或用户交互的相关控件,非常有用。 按此...

    C#ActiveX控件制作及数字签名的处理

    关于c#下ActiveX控件的制作,及给cab加上数字签名,以便在B/S下客户端调用控件,这是本人根据项目需要写的,很详细,参考了很多的网上资源,并将参考的资源也一并打包。希望对下载了的朋友有所帮助。里面还有一个读...

    基于Activex控件的C++、C#混合编程

    这里我首先新建一个基于MFC的Activex控件的工程,把自己要用C++...然后在C#项目(我用的是Winform编程)中直接添加此控件,再通过调用控件的属性、消息或方法(我使用的主要是方法)等接口从而调用了C++所实现的算法。

    C#写的ActiveX控件例子-ASPPing

    练手的玩具,可同时支持域名和IP,直接编译后(或regasm注册)可用于本地调用,IIS里使用需要使用gacutil /i myASPPing.dll加入到程序集缓存。要注意的时加入程序集缓存后修改代码后会无法调试,使用gacutil /U ...

    C#开发ActiveX控件【电子秤源码】

    WEB页获取串口数据读取电子秤的值,使用C#语言的控件对串口进行控制,然后使用JS+AJAX与C#进行交互获得串口数据

    VS2005开发ActiveX控件打包成CAB工具网页调用CAB控件源码

    内含:1:VS2005开发ActiveX控件源码(该控件功能是读取串口数据) ...功能:vs2005c#开发调用ActiveX控件,控件功能读取串口数据(如ID卡读卡器刷卡后可以通过该控件读取。)该控件主要在网页端调用控件读取串口数据。

    C#调用activeX控件方法步骤.docx

    .

    C#实现一个ActiveX控件

    要使用C#实现一个ActiveX控件,需要解决三个问题:  1.使.NET组件能够被COM调用  2.在客户机上注册后,ActiveX控件能通过IE的安全认证  3.未在客户机上注册时,安装包能通过IE的签名认证  本程序的开发环境...

    使用VS2010 C#开发ActiveX控件(上)

    2. 开发ActiveX控件调用封装后的Dll,使用串口通信来对卡进行读写。 3. 打包并发布ActiveX控件。 4. 使用ActiveX控件。 思路1中封装代码有2个类SerialInterfaceHelper,串口通信的帮助类,MifareOneHelper,M1卡...

Global site tag (gtag.js) - Google Analytics