1.功能需求开发一个基于键盘和主机扬声器(小喇叭)的简易电子琴工具,同时它也可以自动的演奏指定的简谱文件。 通过调用计算机系统的API接口,导入kernel32.dll,调用相关的方法,开发基于键盘和扬声器的一个简易电子琴工具。主要是为了了解计算机操作系统中的相关API的知识,学会对API基本应用。 2.实验原理引入[DllImport("kernel32.dll")]和[DllImport("winmm.dll")]。通过设置音频和时间间隔来播放声音。 [DllImport("kernel32.dll")]:是Windows 9x/Me中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理,当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。 [DllImport("winmm.dll")]:是Windows多媒体相关应用程序接口,用于低档的音频和游戏手柄。 3、实现代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace test1
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int freq, int duration);
[DllImport("winmm.dll")]
public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);//播放windows音乐,重载
//进入系统播放音乐
private void m_SystemPlayWav(string strPlayFile)
{
try
{
if (strPlayFile.Trim() == "")
{
return;
}
int SND_FILENAME = 0x00020000;
int SND_ASYNC = 0x0001;
string strPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
strPath = strPath + strPlayFile;
PlaySound(strPath, 0, SND_ASYNC | SND_FILENAME);//播放音乐
}
catch (Exception ex)
{
MessageBox.Show("播放失败","提示");
}
}
private void m_SystemStopPlayWav()
{
try
{
PlaySound(null, 0, 0x40 | 0x04 | 0x02);
}
catch (Exception ex)
{
MessageBox.Show("停止失败", "提示");
}
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
m_SystemPlayWav(comboBox1.SelectedItem.ToString());
}
private void button3_Click(object sender, EventArgs e)
{
m_SystemStopPlayWav();
}
private void button4_Click(object sender, EventArgs e)
{
Beep(400, 300);
}
private void button5_Click(object sender, EventArgs e)
{
Beep(500, 300);
}
private void button6_Click(object sender, EventArgs e)
{
Beep(600, 300);
}
private void button7_Click(object sender, EventArgs e)
{
Beep(700, 300);
}
private void button8_Click(object sender, EventArgs e)
{
Beep(800, 300);
}
private void button9_Click(object sender, EventArgs e)
{
Beep(900, 300);
}
private void button10_Click(object sender, EventArgs e)
{
Beep(1000, 300);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
button4.BackColor = System.Drawing.Color.LightGreen; ;
button4.PerformClick();
button4.BackColor = System.Drawing.Color.Black;
}
if (e.KeyCode == Keys.D2)
{
button5.BackColor = System.Drawing.Color.LightGreen;
button5.PerformClick();
button5.BackColor = System.Drawing.Color.White;
}
if (e.KeyCode == Keys.D3)
{
button6.BackColor = System.Drawing.Color.LightGreen;
button6.PerformClick();
button6.BackColor = System.Drawing.Color.Black;
}
if (e.KeyCode == Keys.D4)
{
button7.BackColor = System.Drawing.Color.LightGreen;
button7.PerformClick();
button7.BackColor = System.Drawing.Color.White;
}
if (e.KeyCode == Keys.D5)
{
button8.BackColor = System.Drawing.Color.LightGreen;
button8.PerformClick();
button8.BackColor = System.Drawing.Color.Black;
}
if (e.KeyCode == Keys.D6)
{
button9.BackColor = System.Drawing.Color.LightGreen;
button9.PerformClick();
button9.BackColor = System.Drawing.Color.White;
}
if (e.KeyCode == Keys.D7)
{
button10.BackColor = System.Drawing.Color.LightGreen;
button10.PerformClick();
button10.BackColor = System.Drawing.Color.Black;
}
if (e.KeyCode == Keys.Enter)
{
button2.PerformClick();//enter键播放
}
if (e.KeyCode == Keys.Space)
{
button3.PerformClick();//space键停止
}
}
}
}
|