1
2 /*This application shows how to play wave files */
3
4 using System;
5 using System.Drawing;
6 using System.Collections;
7 using System.Windows.Forms;
8 using System.Data;
9 using System.Runtime.InteropServices;
10
11
12 namespace WaveFile
13 {
14 /// <summary>
15 /// Summary description for Form1.
16 /// </summary>
17 public class Form1 : System.Windows.Forms.form
18 {
19
20 [DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]
21 public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags
22 flags );
23
24
25 [Flags]
26 public enum PlaySoundFlags : int
27 {
28 // These flags are used by the PlayDound method
29 SND_SYNC = 0x0000,
30 SND_ASYNC = 0x0001,
31 SND_NODEFAULT = 0x0002,
32 SND_LOOP = 0x0008,
33 SND_NOSTOP = 0x0010,
34 SND_NOWAIT = 0x00002000,
35 SND_FILENAME = 0x00020000,
36 SND_RESOURCE = 0x00040004
37 }
38 private System.ComponentModel.Container components;
39 private TextBox textBox1;
40 private Button button1;
41
42 protected void button1_Click (object sender, System.EventArgs e)
43 {
44 OpenFileDialog aFileDialog = new OpenFileDialog();
45 aFileDialog.Title = "Browse to find sound file to play" ;
46 aFileDialog.InitialDirectory = @"c:\" ;
47 aFileDialog.Filter = "Wav Files (*.wav)|*.wav" ;
48 aFileDialog.FilterIndex = 2 ;
49 aFileDialog.RestoreDirectory = true ;
50 if(aFileDialog.ShowDialog() == DialogResult.OK)
51 {
52 textBox1.Text = aFileDialog.FileName ;
53 PlaySound(aFileDialog.FileName ,IntPtr.Zero,PlaySoundFlags.SND_FILENAME |
54 PlaySoundFlags.SND_ASYNC );
55
56 }
57 }
58 /// <summary>
59 /// Required designer variable.
60 /// </summary>
61 // private System.ComponentModel.Container components = null;
62
63 public Form1()
64 {
65 //
66 // Required for Windows Form Designer support
67 //
68 InitializeComponent();
69
70 //
71 // TODO: Add any constructor code after InitializeComponent call
72 //
73 }
74
75 /// <summary>
76 /// Clean up any resources being used.
77 /// </summary>
78 protected override void Dispose( bool disposing )
79 {
80 if( disposing )
81 {
82 if (components != null)
83 {
84 components.Dispose();
85 }
86 }
87 base.Dispose( disposing );
88 }
89
90 #region Windows Form Designer generated code
91 /// <summary>
92 /// Required method for Designer support - do not modify
93 /// the contents of this method with the code editor.
94 /// </summary>
95 private void InitializeComponent()
96 {
97 this.button1 = new System.Windows.Forms.Button();
98 this.textBox1 = new System.Windows.Forms.TextBox();
99 this.SuspendLayout();
100 //
101 // button1
102 //
103 this.button1.Location = new System.Drawing.Point(192, 40);
104 this.button1.Name = "button1";
105 this.button1.Size = new System.Drawing.Size(88, 24);
106 this.button1.TabIndex = 0;
107 this.button1.Text = "Browse";
108 this.button1.Click += new System.EventHandler(this.button1_Click);
109 //
110 // textBox1
111 //
112 this.textBox1.Location = new System.Drawing.Point(8, 40);
113 this.textBox1.Name = "textBox1";
114 this.textBox1.Size = new System.Drawing.Size(168, 20);
115 this.textBox1.TabIndex = 1;
116 this.textBox1.Text = "FIle path";
117 //
118 // Form1
119 //
120 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
121 this.ClientSize = new System.Drawing.Size(292, 266);
122 this.Controls.Add(this.textBox1);
123 this.Controls.Add(this.button1);
124 this.Name = "Form1";
125 this.Text = "Platform Invoke WinSound C#";
126 this.ResumeLayout(false);
127 this.PerformLayout();
128 }
129 #endregion
130
131 /// <summary>
132 /// The main entry point for the application.
133 /// </summary>
134 [STAThread]
135 static void Main()
136 {
137 Application.Run(new Form1());
138 }
139 }
140 }
|