.Net FrameWork SDK中并没有包含Direct X SDK,所以为了顺利的完成本文后面介绍, 必须先下载、安装Direct X SDK。具体的下载地址为:http://download.microsoft.com/download/a/c/d/acdfb557-266f-4af4-8673-6ab8009b4ddd/dxsdk_apr2005.exe。此版本是2005四月份推出的英文版。这并不妨碍下面的程序实现。安装完Direct X后,就会在存在"C:/WINDOWS/Microsoft.NET/Managed DirectX"这样一个目录,此目录中目录中应该有九个DLL文件和九个XML文件。九个DLL文件大致对应于DirectX中的十个命名空间。编程中就是使用其中的命名空间来提供对输入设备、声音、网络播放、图形等的支持。Direct X SDK 9.0中的定义的命名空间及其主要的作用具体如表01所示:
命名空间
描述
Microsoft.DirectX
公共类和数学结构
Microsoft.DirectX.Direct3D
3D图形和助手库
Microsoft.DirectX.DirectDraw
Direct Draw 图形API。这是旧式命名空间,现在已经不需要使用它。
Microsoft.DirectX.DirectPlay
用于多玩家游戏的网络API
Microsoft.DirectX.DirectSound
声音支持
Microsoft.DirectX.DirectInput
输入设备支持(例如,鼠标和游戏杆)
Microsoft.DirectX.AudioVideoPlayback
播放视频和音频(例如,在电脑上播放各自视频动画文件)
Microsoft.DirectX.Diagnostics
疑难解答
Microsoft.DirectX.Security
访问安全性
Microsoft.DirectX.Security.Permissions
访问安全权限
表01:Direct X SDK9.0中的定义的命名空间及其主要的作用
Direct X内容十分丰富,下文介绍的只是其中的一个小的应用,即用Visual C#调用Direct X中的SDK来播放视频文件为例子,程序中只使用了命名空间"Microsoft.DirectX.AudioVideoPlayback"。 Microsoft.DirectX.AudioVideoPlayback命名空间中定义了三个类:"Audio"、"Video"和"TextureRenderEventArgs"。其中前二个类是最常用的,分别用来支持音频和视频。Microsoft.DirectX.AudioVideoPlayback命名空间中定义的常用属性及其具体的作用可参阅表02所示:
属性
描述
Audio
获取视频文件中的音频对象,可用来后续的音频播放
Caption
获取或设置在Form上播放视频的名称
CurrentPosition
获取或设置播放视频的当前位置
DefaultSize
获取播放视频的缺省的视频大小
Fullscreen
获取或设置视频文件是否在全屏模式下播放
IsCursorHidden
获取播放的视频时鼠标的状态:隐藏或显示
Owner
获取或设置视频播放的宿主组件
Paused
获取当前的播放状态是否处于暂停状态
Playing
获取当前的播放状态是否处于播放状态.
SeekingCaps
获取是否可以搜索性能
Size
Retrieves and sets the size of the video for playback.
8. 选中【添加引用】菜单后,弹出【添加引用】对话框,按照图02所示,在【选定的组件】栏目中加入"Microsoft.DirectX.AudioVideoPlayback"后,单击【确定】按钮,则引用"Microsoft.DirectX.AudioVideoPlayback"文件成功。这是因为Visual Studio .Net的缺省编译环境中没有"Microsoft.DirectX.AudioVideoPlayback.dll"文件,而程序中却使用到"Microsoft.DirectX.AudioVideoPlayback"命名空间,所以要在编译环境中引用"Microsoft.DirectX.AudioVideoPlayback"所在的文件。
9. 把Visual Studio .Net的当前窗口切换到【Form1.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往设计窗体中拖入下列组件,并执行相应操作:
10. 把Visual Studio .Net的当前窗口切换到Form1.cs的代码编辑窗口,在Form1.cs的首部的引入命名空间的代码区中,用下列代码替换Form1.cs中由系统自动产生的引入命名空间代码:
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using Microsoft.DirectX.AudioVideoPlayback ; //引入视频播放所要使用的Direct X命名空间