⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 PPC 上 播放声音的一些测试使用
💻 CS
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using PInvokeLibrary;
using System.Runtime.InteropServices;

namespace testVoice
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnPlayBg;
		private System.Windows.Forms.Button btnStopPlayBg;
		private System.Windows.Forms.Button btnBomb;
		WaveOut wo = null;
		WaveOut wo2 = null;		string status="stop";		Thread thread, tempthread;
		String fileName1,fileName2;
		MemoryStream strm;
		WaveOut test;
		Sound sndback,sndbomb=null;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;

		public bool landscape=false;
		private System.Windows.Forms.Button button3;
		int changed=1;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			fileName1 = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
			fileName1 = Path.GetDirectoryName(fileName1);
			fileName1 = Path.Combine(fileName1, "gaming.wav");

			fileName2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
			fileName2 = Path.GetDirectoryName(fileName2);
			fileName2 = Path.Combine(fileName2, "block.wav");

			strm=(MemoryStream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("testVoice.explosion.wav");
			if(wo==null)			{				wo=new WaveOut();			}			if(wo2==null)			{				wo2=new WaveOut();			}

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{			if(wo!=null)			{				if(!wo.Done())				{					wo.Stop();				}				wo.Dispose();			}			if(wo2!=null)			{				if(!wo2.Done())				{					wo2.Stop();				}				wo2.Dispose();			}			thread=null;
			base.Dispose( disposing );
		}
		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnPlayBg = new System.Windows.Forms.Button();
			this.btnStopPlayBg = new System.Windows.Forms.Button();
			this.btnBomb = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			// 
			// btnPlayBg
			// 
			this.btnPlayBg.Location = new System.Drawing.Point(56, 24);
			this.btnPlayBg.Size = new System.Drawing.Size(96, 24);
			this.btnPlayBg.Text = "播放背景音乐";
			this.btnPlayBg.Click += new System.EventHandler(this.btnPlayBg_Click);
			// 
			// btnStopPlayBg
			// 
			this.btnStopPlayBg.Location = new System.Drawing.Point(56, 64);
			this.btnStopPlayBg.Size = new System.Drawing.Size(96, 24);
			this.btnStopPlayBg.Text = "停止背景音乐";
			this.btnStopPlayBg.Click += new System.EventHandler(this.btnStopPlayBg_Click);
			// 
			// btnBomb
			// 
			this.btnBomb.Location = new System.Drawing.Point(152, 24);
			this.btnBomb.Size = new System.Drawing.Size(72, 24);
			this.btnBomb.Text = "响声";
			this.btnBomb.Click += new System.EventHandler(this.btnBomb_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(32, 144);
			this.button1.Text = "90";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(112, 232);
			this.button2.Text = "Exit";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(128, 144);
			this.button3.Text = "0";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			// 
			// Form1
			// 
			this.ControlBox = false;
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnBomb);
			this.Controls.Add(this.btnPlayBg);
			this.Controls.Add(this.btnStopPlayBg);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Text = "Test Voice";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Resize += new System.EventHandler(this.Form1_Resize);
			this.GotFocus += new System.EventHandler(this.Form1_GotFocus);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.LostFocus += new System.EventHandler(this.Form1_LostFocus);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		
		public void run()
		{			while(true)			{				Application.DoEvents();				Thread.Sleep(200);				if(status=="playing")				{					if(wo.Done())					{						wo.Play(fileName1,0,0xffff,0xffff);					}//					if(sndback==null)//					{//						sndback=new Sound(fileName1);//					}//					sndback.Play();				}				Application.DoEvents();
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			thread= new Thread(new System.Threading.ThreadStart(run));			thread.Start();
		}

		private void btnPlayBg_Click(object sender, System.EventArgs e)
		{
			this.status="playing";
		}

		private void btnStopPlayBg_Click(object sender, System.EventArgs e)
		{
			this.status="stop";
//			if(this.sndback!=null)
//			{
//				this.sndback.endPlay();
//			}
			if(wo!=null)
			{
				this.wo.Stop();
//				while(!wo.Done())
//				{
//					wo.BlockDone(IntPtr.Zero);
//				}
			}
		}

		private void btnBomb_Click(object sender, System.EventArgs e)
		{//			if(sndbomb==null)//			{//				sndbomb=new Sound(fileName2);//			}//			sndbomb.endPlay();//			sndbomb.PlayOnce();////			if(!wo2.Done())//			{
//				wo2.Stop();//			}//			if(wo2.Done())//			{
//				strm=(MemoryStream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("testVoice.block.wav");//				wo2.Play(strm,0,0xffff,0xffff);//			}			//playmusic();
//			strm=(MemoryStream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("testVoice.explosion.wav");
			tempthread= new Thread(new System.Threading.ThreadStart(play));			tempthread.Start();
		}

		public void playmusic()
		{
			WaveOut wo0=new WaveOut();			wo0.Play(fileName2,16*1024,0xffff,0xffff);
			while(!wo0.Done())
			{
			}
			wo0.Dispose();
		}

		public void play()
		{			test=new WaveOut();
			//MemoryStream strm=(MemoryStream)System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("testVoice.explosion.wav");			test.Play(strm,0,0xffff,0xffff);
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			testVoice.ScreenMode sm=new ScreenMode();
			this.button1.Text=sm.ChangeMode(90);
//			Mode.DevMode devMode=new Mode.DevMode();
//			devMode.dmSize=(ushort)System.Runtime.InteropServices.Marshal.SizeOf(devMode); 
//			devMode.dmFields = (uint)Mode.DM_DISPLAYORIENTATION;
//			devMode.dmDisplayOrientation =(uint) Mode.DMDO_90;
//			if(ChangeDisplaySettingsEx(null,ref devMode,IntPtr.Zero,(uint)Mode.CDS_TEST,IntPtr.Zero)==(int)Mode.DISP_CHANGE_SUCCESSFUL)
//			{
//				this.button1.Text="success";
//			}
//			else
//			{
//				this.button1.Text="Failed";
//			}
		}
//		private enum Flags 
//		{
//			DMDO_0= 0,
//			DMDO_90=1,
//			DMDO_180=2,
//			DMDO_270=4,
//			CDS_TEST= 0x00000002,
//			CDS_VIDEOPARAMETERS=0x00000020,
//			CDS_RESET=0x40000000,
//			DISP_CHANGE_SUCCESSFUL=0,
//			DISP_CHANGE_RESTART=1,
//			DISP_CHANGE_FAILED=-1,
//			DISP_CHANGE_BADMODE=-2,
//			DISP_CHANGE_NOTUPDATED=-3,
//			DISP_CHANGE_BADFLAGS=-4,
//			DISP_CHANGE_BADPARAM=-5
//			
//		}
//
//		private enum DmdoF:long
//		{
//			DM_ORIENTATION=0x00000001L,
//			DM_PAPERSIZE=0x00000002L,
//			DM_PAPERLENGTH=0x00000004L,
//			DM_PAPERWIDTH=0x00000008L,
//			DM_SCALE =0x00000010L,
//			DM_COPIES=0x00000100L,
//			DM_DEFAULTSOURCE=0x00000200L,
//			DM_PRINTQUALITY=0x00000400L,
//			DM_COLOR=0x00000800L,
//			DM_DUPLEX=0x00001000L,
//			DM_YRESOLUTION= 0x00002000L,
//			DM_TTOPTION =0x00004000L,
//			DM_COLLATE= 0x00008000L,
//			DM_FORMNAME = 0x00010000L,
//			DM_LOGPIXELS = 0x00020000L,
//			DM_BITSPERPEL = 0x00040000L,
//			DM_PELSWIDTH = 0x00080000L,
//			DM_PELSHEIGHT = 0x00100000L,
//			DM_DISPLAYFLAGS = 0x00200000L,
//			DM_DISPLAYFREQUENCY= 0x00400000L,
//			DM_DISPLAYORIENTATION= 0x00800000L,
//			DM_DISPLAYQUERYORIENTATION= 0x01000000L
//		}

//		public struct DEVMODE
//		{
//			public sbyte[] dmDeviceName;
//			public ushort dmSpecVersion;
//			public ushort dmDriverVersion;
//			public ushort dmSize;
//			public ushort dmDriverExtra;
//			public uint dmFields;
//			public short dmOrientation;
//			public short dmPaperSize;
//			public short dmPaperLength;
//			public short dmPaperWidth;
//			public short dmScale;
//			public short dmCopies;
//			public short dmDefaultSource;
//			public short dmPrintQuality;
//			public short dmColor;
//			public short dmDuplex;
//			public short dmYResolution;
//			public short dmTTOption;
//			public short dmCollate;
//			public sbyte[] dmFormName;
//			public ushort dmLogPixels;
//			public uint dmBitsPerPel;
//			public uint dmPelsWidth;
//			public uint dmPelsHeight;
//			public uint dmDisplayFlags;
//			public uint dmDisplayFrequency;
//			public uint dmDisplayOrientation;
//		}
//
//		[DllImport("coredll.dll", EntryPoint="ChangeDisplaySettingsEx",SetLastError=true)]
//		internal static extern int ChangeDisplaySettingsEx(
//			string lpszDeviceName,
//			ref Mode.DevMode lpDevMode,
//			IntPtr hwnd,
//			uint dwflags,
//			IntPtr lParam);

		private void button2_Click(object sender, System.EventArgs e)
		{
			if(landscape)
			{
					testVoice.ScreenMode sm=new ScreenMode();
					this.button1.Text=sm.ChangeMode(90);
			}
			if(wo!=null)
			{
				wo.Stop();
			}
			this.Close();
			Application.Exit();
		}

		private void Form1_Resize(object sender, System.EventArgs e)
		{
			if(changed ==1 || (changed==2 && this.landscape==true))
			{
				if(Screen.PrimaryScreen.Bounds.Width>Screen.PrimaryScreen.Bounds.Height)
				{
					testVoice.ScreenMode sm=new ScreenMode();
					this.button1.Text=sm.ChangeMode(180);
					this.landscape=true;
					changed=2;
				}
				else
				{
					if(changed ==1)
					{
						this.landscape=false;
					}
					else	//第二次 就不要变化了.且保持原来的landscape状态。
					{
						changed=1;
					}
				}
			}
			else	//否则不需要变化,changed=1;
			{
				changed=1;
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			testVoice.ScreenMode sm=new ScreenMode();
			this.button1.Text=sm.ChangeMode(0);
		}

		private void Form1_GotFocus(object sender, System.EventArgs e)
		{
			if(wo!=null)
			{
				this.wo.Resume();
			}
		}

		private void Form1_LostFocus(object sender, System.EventArgs e)
		{
			if(wo!=null)
			{
				this.wo.Pause();
			}
		}
	}
}

⌨️ 快捷键说明

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