这个小项目是我在学习C#winform的时候做的,不得不承认C#在做桌面程序程序是Java不能比的,如果有VS这样的开发工具就如虎添翼了,我这项目是用VS2015做的,用VS2010打包安装(后来的VS都不具备这个功能了)。下面说说具体这个语言计算器,这个计算机实现了键盘输入、实现了语音播报这两个比较有特色的功能,下面贴张图片:
其实键盘输入和语音播报分别设计到C#的两个概念:键盘监听事件和播放音频文件,下面主要首先为大家介绍如何实现鼠标监听,我就不废话了,直接贴项目鼠标监听的代码了,如下
C#实现鼠标监听
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)//监听键盘事件
{
if (keyData == Keys.D1 || keyData == Keys.NumPad1) GSH("1"); if (keyData == Keys.D2 || keyData == Keys.NumPad2) GSH("2"); if (keyData == Keys.D3 || keyData == Keys.NumPad3) GSH("3"); if (keyData == Keys.D4 || keyData == Keys.NumPad4) GSH("4");
if (keyData == Keys.D5 || keyData == Keys.NumPad5) GSH("5"); if (keyData == Keys.D6 || keyData == Keys.NumPad6) GSH("6"); if (keyData == Keys.D7 || keyData == Keys.NumPad7) GSH("7"); if (keyData == Keys.D8 || keyData == Keys.NumPad8) GSH("8");
if (keyData == Keys.D9 || keyData == Keys.NumPad9) GSH("9"); if (keyData == Keys.D0 || keyData == Keys.NumPad0) GSH("0"); if (keyData == Keys.A && radioButton3.Checked) GSH("A"); if (keyData == Keys.B && radioButton3.Checked) GSH("B");
if (keyData == Keys.C && radioButton3.Checked) GSH("C"); if (keyData == Keys.D && radioButton3.Checked) GSH("D");
if (keyData == Keys.E && radioButton3.Checked) GSH("E"); if (keyData == Keys.F && radioButton3.Checked) GSH("F");
if (keyData == Keys.Space || keyData == Keys.Decimal) GSH(".");
if (keyData == Keys.Enter)
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_007.wav");//播放声音Voice_007.wav,下同
sndPlayer.Play();
}
countans++;
switch (fh)
{
case '+': Move(2); break;
case '-': Move(3); break;
case 'x': Move(4); break;
case '/': Move(5); break;
}
}
if(keyData==Keys.Delete)//判断键盘是否输入Delete键
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_005.wav");
sndPlayer.Play();
}
richTextBox1.Text = GL;
Data = "";
Data1 = "";
str1 = "\n\n";
lengs = GL.Length - 2;
}
if(keyData==Keys.Back)
{
string str1 = "\n\n";
if (lengs <= GL.Length - 2)
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_006.wav");
sndPlayer.Play();
}
lengs++;
for (int i = 0; i < lengs; i++)
{
str1 += ' ';
}
if (Data.Length > 0)
Data = Data.Remove(Data.Length - 1, 1);
richTextBox1.Text = str1 + Data;
if (Data.Length == 0)
{
richTextBox1.Text = GL;
lengs = GL.Length - 2;
}
}
}
if(keyData==Keys.Subtract || keyData==Keys.Down)
{
if (!Data.Equals(""))
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_002.wav");
sndPlayer.Play();
}
x = double.Parse(Data);//将data转化成字符型
fh = '-';
Move(1);
}
}//减
if (keyData == Keys.Multiply || keyData == Keys.Left)
{
if (!Data.Equals(""))
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_003.wav");
sndPlayer.Play();
}
x = double.Parse(Data);
fh = 'x';
Move(1);
}
}
if (keyData == Keys.Add || keyData == Keys.Up)
{
if (!Data.Equals(""))
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_001.wav");
sndPlayer.Play();
}
x = double.Parse(Data);
fh = '+';
Move(1);
}
}
if (keyData == Keys.Divide || keyData == Keys.Right)
{
if (!Data.Equals(""))
{
if (sound == 1)
{
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_004.wav");
sndPlayer.Play();
}
x = double.Parse(Data);
fh = '/';
Move(1);
}
}
return true;
//return base.ProcessCmdKey(ref msg, keyData);
}
接下来为大家介绍如何在C#中播放音频文件,也给大家看看代码吧,相信你们一看就会,如下:
C#winform播放音频文件(点击这里查看C#播放声音详情)
System.Media.SoundPlayer sndPlayer = new
System.Media.SoundPlayer(Application.StartupPath + @"/Voice_004.wav");//Voice_004.wav是位于根目录下的音频文件
sndPlayer.Play();
好了,这个项目就为大家介绍到这里,如需源码,如下是下载网址:
http://pan.baidu.com/s/1sl9ySix