《C#-键盘钢琴源代码(3页).docx》由会员分享,可在线阅读,更多相关《C#-键盘钢琴源代码(3页).docx(3页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-C#-键盘钢琴源代码-第 3 页C# 键盘钢琴源代码开发平台:VS2008 windows窗体程序功能设计:当窗体获得焦点时,按下键盘可发出声音,一个字母对应一个声音,音高从A到Z由低到高排列,同时对应26个button,也可用鼠标单击,功能比较简单但很实用。Form1.cs设计:新建一个Sound类,用于播放音乐(注:貌似VS2008只支持.wav格式的音乐文件)Class1.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.DirectX
2、.DirectSound;/注,需下载DirectX插件namespace KeyboardPiano public class Sound : IDisposable private SecondaryBuffer mSecondaryBuffer = null; /private bool mDisposed = false; public Sound(string filePath, Form1 fm) BufferDescription desc = new BufferDescription(); desc.StaticBuffer = true; Device d = new De
3、vice(); d.SetCooperativeLevel(fm, CooperativeLevel.Normal); mSecondaryBuffer = new SecondaryBuffer(filePath, desc, d); public void Play() mSecondaryBuffer.Play(0, BufferPlayFlags.Default); #region IDisposable 成员 public void Dispose() mSecondaryBuffer.Dispose(); mSecondaryBuffer = null; #endregionFor
4、m1.cs:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Media;using System.Threading;using System.IO;namespace KeyboardPiano public partial class Form1 : Form pu
5、blic Form1() InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; /按钮事件 private void button1_Click(object sender, EventArgs e) Sound a = new Sound(C:Documents and Settings桌面wavc-1.wav,this); a.Play(); /省略中间的按钮 private void button26_Click(object sender, EventArgs e) Sound z = new Sound(C:Documents and Settings桌面wavd3.wav, this); z.Play(); /键盘事件 private void Form1_KeyDown(object sender, KeyEventArgs e) switch(e.KeyData) case Keys.A: button20_Click(sender, e); break; /其他键省略 case Keys.Z: button26_Click(sender, e); break;
限制150内