这是给directX初学者的教程,如果你是大虾,大可不必理会本文。
第1步:下载并安装DirectX 9 SDK
DirectX 9 SDK下载地址:http://msdn.microsoft.com/directx/sdk/ ,目前最新版本是2006年8月,下载包500MB多。
第2步:建立C#应用程序
新建一个C#的windows应用程序,名称这里输mydirectXtest。
解决方案管理器里,右击项目,“添加引用”,选中DirectX和DirectSound,如下图:在Form1.cs里面添加:
往Form1上面拉一个Label和一个Button,在Button onclick事件里面写入:
Microsoft.DirectX.DirectSound.Device dev = new Microsoft.DirectX.DirectSound.Device(); ——建立device的类;dev.SetCooperativeLevel(this, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); ——设置CooperativeLevel。因为Windows是多任务的系统,设备不是独占的,所以在使用设备前要为这个设备设置CooperativeLevel。调用Device的SetCooperativeLevel方法:其中,第一个参数是一个Control;第二个参数是个枚举类型,用来设置优先级的。SecondaryBuffer snd = new Microsoft.DirectX.DirectSound.SecondaryBuffer("../../NewDrums.wav", dev); —— 开辟缓冲区。声音设备有个自己的缓冲区,叫主缓冲区。系统中,一个设备有唯一的主缓冲区。由于windows是多任务的,所以可以有几个程序同时利用一个设备播放声音,每个程序都自己开辟一个二级缓冲区,放自己的声音。 这里需要注意播放声音的路径,一开始初学者容易把wav声音放到项目里面,在SecondaryBuffer里面直接写“NewDrums.wav”,调试是会显示“应用程序错误”。因为调试的默认文件夹是Debug,需要的声音文件应该放到Debug目录下,用“NewDrums.wav”的格式;或者放在项目下面,用“../../NewDrums.wav”的格式。很傻的错误吧。这样,调试程序,按button就会播放声音了。全部代码如下:Form1.cs:
posted on 2006-08-30 11:53 zenith 阅读(9129) 评论(9) 编辑 收藏 网摘 所属分类: C#
觉得好玩。。。做了一下 支持 回复 引用
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; namespace myDirectXTest { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button btnOk; private System.Windows.Forms.Label label1; /// <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 ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btnOk = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnOk // this.btnOk.Location = new System.Drawing.Point(184, 64); this.btnOk.Name = "btnOk"; this.btnOk.Size = new System.Drawing.Size(72, 24); this.btnOk.TabIndex = 0; this.btnOk.Text = "OK"; this.btnOk.Click += new System.EventHandler(this.btnOk_Click); // // label1 // this.label1.Location = new System.Drawing.Point(64, 56); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(80, 32); this.label1.TabIndex = 1; this.label1.Text = "label1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(304, 277); this.Controls.Add(this.label1); this.Controls.Add(this.btnOk); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void btnOk_Click(object sender, System.EventArgs e) { //建立声音设备 Microsoft.DirectX.DirectSound.Device dv = new Device(); dv.SetCooperativeLevel(this, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); //为声音建立二级缓冲区 try { Microsoft.DirectX.DirectSound.SecondaryBuffer snd = new SecondaryBuffer("E:\\san\\C#\\520show_Blogs_Z-CH\\myDirectXTest\\myDirectXTest\\bin\\Debug魔比斯环.wmv", dv); //播放声音 snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default); } catch(Exception ee) { this.label1.Text = ee.Message.ToString(); } } } } 我写的怎么不能放啊? 回复 引用
是wmv的格式啊,你先换wav看行不? 回复 引用 查看
楼主会不会在Form上DrawText啊?想学不DirectDraw。谢谢。 回复 引用 查看
听说DirectDraw是过时的,微软不推荐用它。为什么不用Microsoft.DirectX.Direct3D来实现DrawText功能呢? 回复 引用 查看
第一次接触DX, 还以为DX只能在C++和VB下用, 看到这个例子,真是一文惊醒梦中人,这个例子非常好,写的很详细,支持楼主,期待更好的教程 回复 引用 查看
不知道楼主 能不能写一篇如何安装和配置directX的文章 谢谢 回复 引用
请问可以支持MP3格式吗?都支持什么格式的音乐文件啊,加我QQ22550450,给我发邮件也可以,谢谢 回复 引用
请问可以支持那些格式的音乐文件 回复 引用 查看
昵称: [登录] [注册]
主页:
邮箱:(仅博主可见)
验证码: 看不清,换一个
评论内容:
登录 注册
[使用Ctrl+Enter键快速提交评论]
Powered by: 博客园 Copyright © zenith