MainForm.cs
上传用户:linger1010
上传日期:2008-12-08
资源大小:561k
文件大小:2k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace PlayingSound
- {
- public partial class MainForm : Form
- {
- private const UInt32 SND_SYNC = 0x00000000;
- private const UInt32 SND_ASYNC = 0x00000001;
- private const UInt32 SND_NODEFAULT = 0x00000002;
- private const UInt32 SND_MEMORY = 0x00000004;
- private const UInt32 SND_LOOP = 0x00000008;
- private const UInt32 SND_NOSTOP = 0x00000010;
- private const UInt32 SND_NOWAIT = 0x00002000;
- private const UInt32 SND_ALIAS = 0x00010000;
- private const UInt32 SND_FILENAME = 0x00020000;
- private const UInt32 SND_RESOURCE = 0x00040004;
- public MainForm()
- {
- InitializeComponent();
- }
- [DllImport("coredll.dll",
- CallingConvention = CallingConvention.Winapi,
- CharSet = CharSet.Unicode,
- EntryPoint = "PlaySound",
- PreserveSig = true,
- SetLastError = false)]
- private extern static bool PlaySound(
- String pszSound,
- IntPtr hmod,
- UInt32 fdwSound);
- private void m_btnBrowse_Click(object sender, EventArgs e)
- {
- if(DialogResult.OK != m_openFileDialog.ShowDialog())
- return;
- m_txtFile.Text = m_openFileDialog.FileName;
- }
- // “Play Once”按钮
- private void m_btnPlayOnce_Click(object sender, EventArgs e)
- {
- // 播放指定的文件一次
- PlaySound(m_txtFile.Text, IntPtr.Zero, SND_ASYNC | SND_FILENAME);
- }
- // “Play Loop”按钮
- private void m_btnPlayLoop_Click(object sender, EventArgs e)
- {
- // 循环播放指定的文件
- PlaySound(m_txtFile.Text, IntPtr.Zero, SND_ASYNC | SND_FILENAME | SND_LOOP);
- }
- // “Stop”按钮
- private void m_btnStop_Click(object sender, EventArgs e)
- {
- // 将第一个参数设置为null可以终止声音的播放
- PlaySound(null, IntPtr.Zero, SND_ASYNC | SND_FILENAME);
- }
- }
- }