您的位置:首页 > 编程语言 > C#

在C#中调用API播放windows声音

2006-05-11 20:43 597 查看
这个函数能发出系统缺省的警告声.

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern int MessageBeep(uint n);

private void button1_Click(object sender, System.EventArgs e)
{

MessageBeep(0xFFFFFFFF);
}

 

----===============================================

using System;
using System.Runtime.InteropServices;

namespace PlaySound
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
  //导入 Windows Beep() API 函数
  [DllImport("kernel32.dll")]
  private static extern bool Beep(int freq, int dur);

  // 定义PlaySound()要使用的常数
  public const int SND_FILENAME = 0x00020000;
  public const int SND_ASYNC = 0x0001;

  // 导入 Windows PlaySound() 函数
  [DllImport("winmm.dll")]
  public static extern bool PlaySound(string pszSound,
   int hmod,
   int fdwSound);

  [STAThread]
  static void Main(string[] args)
  {
   // 使用Ctrl+g发出蜂鸣声
   Console.Write("/a");
   Console.WriteLine("使用Ctrl+g发出蜂鸣声...");
   Console.ReadLine();

   // 使用 Windows API 发出蜂鸣声
   Beep(800, 200);
   Console.WriteLine("使用 Windows API 发出蜂鸣声...");
   Console.ReadLine();

   // 播放bells.wav文件
   PlaySound("bells.wav",
    0,
    SND_FILENAME | SND_ASYNC);
   Console.WriteLine("播放bells.wav文件...");
   Console.ReadLine();
  }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息