form2.cs
来自「C#声卡的检测及声卡发声源代码」· CS 代码 · 共 155 行
CS
155 行
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.Threading;
using System.Runtime.InteropServices;
namespace PC_ShengKa
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
//定义枚举类型Tone
protected enum Tone
{
REST = 0,
A = 220,
B = 247,
C = 262,
D = 294,
E = 330,
F = 349,
G = 392,
}
//定义枚举类型Duration
protected enum Duration
{
WHOLE = 1200,
HALF = WHOLE / 2,
QUARTER = HALF / 2,
EIGHTH = QUARTER / 2,
SIXTEENTH = EIGHTH / 2,
}
//定义结构体Note
protected struct Note
{
Tone toneVal;
Duration durVal;
public Note(Tone frequency, Duration time)
{
toneVal = frequency;
durVal = time;
}
public Tone NoteTone { get { return toneVal; } set { toneVal = value; } }
public Duration NoteDuration { get { return durVal; } set { durVal = value; } }
}
//制件喇叭
protected void Play(Note tune)
{
if (tune.NoteTone == Tone.REST)
Thread.Sleep((int)tune.NoteDuration);
else
Console.Beep((int)tune.NoteTone, (int)tune.NoteDuration);
}
private void PlayMic(int a)
{
Note note = new Note();
switch (a)
{
case 1:
note.NoteTone = Tone.A;
break;
case 2:
note.NoteTone = Tone.B;
break;
case 3:
note.NoteTone = Tone.C;
break;
case 4:
note.NoteTone = Tone.D;
break;
case 5:
note.NoteTone = Tone.E;
break;
case 6:
note.NoteTone = Tone.F;
break;
case 7:
note.NoteTone = Tone.G;
break;
default:
break;
}
if (this.radioButton1.Checked)
{
note.NoteDuration = Duration.WHOLE;
}
else if (this.radioButton2.Checked)
{
note.NoteDuration = Duration.HALF;
}
else if (this.radioButton3.Checked)
{
note.NoteDuration = Duration.QUARTER;
}
else if (this.radioButton4.Checked)
{
note.NoteDuration = Duration.EIGHTH;
}
else if (this.radioButton5.Checked)
{
note.NoteDuration = Duration.SIXTEENTH;
}
Play(note);
}
private void button1_Click(object sender, EventArgs e)
{
PlayMic(1);
}
private void button2_Click(object sender, EventArgs e)
{
PlayMic(2);
}
private void button3_Click(object sender, EventArgs e)
{
PlayMic(3);
}
private void button4_Click(object sender, EventArgs e)
{
PlayMic(4);
}
private void button5_Click(object sender, EventArgs e)
{
PlayMic(5);
}
private void button6_Click(object sender, EventArgs e)
{
PlayMic(6);
}
private void button7_Click(object sender, EventArgs e)
{
PlayMic(7);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?