mainform.cs

来自「《Windows Mobile平台应用与开发》源码」· CS 代码 · 共 70 行

CS
70
字号
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);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?