Csharp/C Sharp/Development Class/Sound — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:43, 26 мая 2010
Noice Maker
/*
Code revised from chapter 1
GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward
Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604
Sample chapter
http://www.packtpub.ru/files/1604_CustomControls_SampleChapter.pdf
For More info on GDI+ Custom Control with Microsoft Visual C# book
visit website www.packtpub.ru
*/
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.IO;
public partial class Form1 : Form
{
private TinyNoiseMaker tinyNoiseMaker1;
private System.Windows.Forms.Button playButton;
private System.ruponentModel.IContainer components = null;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.playButton = new System.Windows.Forms.Button();
this.tinyNoiseMaker1 = new TinyNoiseMaker();
this.SuspendLayout();
//
// playButton
//
this.playButton.Location = new System.Drawing.Point(100, 22);
this.playButton.Name = "playButton";
this.playButton.Size = new System.Drawing.Size(82, 23);
this.playButton.TabIndex = 1;
this.playButton.Text = "Play My File";
this.playButton.UseVisualStyleBackColor = true;
this.playButton.Click += new System.EventHandler(this.playButton_Click);
//
// tinyNoiseMaker1
//
this.tinyNoiseMaker1.BackColor = System.Drawing.SystemColors.ControlLight;
this.tinyNoiseMaker1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tinyNoiseMaker1.FileName = null;
this.tinyNoiseMaker1.Location = new System.Drawing.Point(21, 68);
this.tinyNoiseMaker1.Name = "tinyNoiseMaker1";
this.tinyNoiseMaker1.Size = new System.Drawing.Size(268, 37);
this.tinyNoiseMaker1.TabIndex = 0;
this.tinyNoiseMaker1.PlayStart += new System.EventHandler(this.tinyNoiseMaker1_PlayStart);
this.tinyNoiseMaker1.PlayStop += new System.EventHandler(this.tinyNoiseMaker1_PlayStop);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(316, 163);
this.Controls.Add(this.playButton);
this.Controls.Add(this.tinyNoiseMaker1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private void tinyNoiseMaker1_PlayStart(object sender, EventArgs e)
{
Text = "Started Play of: " + tinyNoiseMaker1.FileName;
}
private void tinyNoiseMaker1_PlayStop(object sender, EventArgs e)
{
Text = "Stopped Play of : " + tinyNoiseMaker1.FileName;
}
private void playButton_Click(object sender, EventArgs e)
{
tinyNoiseMaker1.FileName = "C:\\WINDOWS\\Media\\tada.wav";
tinyNoiseMaker1.Play();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public partial class TinyNoiseMaker : UserControl
{
public event EventHandler PlayStart;
public event EventHandler PlayStop;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.openButton = new System.Windows.Forms.Button();
this.playButton = new System.Windows.Forms.Button();
this.stopButton = new System.Windows.Forms.Button();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// openButton
//
this.openButton.Location = new System.Drawing.Point(4, 4);
this.openButton.Name = "openButton";
this.openButton.Size = new System.Drawing.Size(82, 25);
this.openButton.TabIndex = 0;
this.openButton.Text = "Open";
this.openButton.UseVisualStyleBackColor = true;
this.openButton.Click += new System.EventHandler(this.openButton_Click);
//
// playButton
//
this.playButton.Location = new System.Drawing.Point(86, 4);
this.playButton.Name = "playButton";
this.playButton.Size = new System.Drawing.Size(82, 25);
this.playButton.TabIndex = 1;
this.playButton.Text = "Play";
this.playButton.UseVisualStyleBackColor = true;
this.playButton.Click += new System.EventHandler(this.playButton_Click);
//
// stopButton
//
this.stopButton.Location = new System.Drawing.Point(168, 4);
this.stopButton.Name = "stopButton";
this.stopButton.Size = new System.Drawing.Size(82, 25);
this.stopButton.TabIndex = 2;
this.stopButton.Text = "Stop";
this.stopButton.UseVisualStyleBackColor = true;
this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
//
// openFileDialog
//
this.openFileDialog.FileName = "openFileDialog";
//
// TinyNoiseMaker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ControlLight;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Controls.Add(this.stopButton);
this.Controls.Add(this.playButton);
this.Controls.Add(this.openButton);
this.Name = "TinyNoiseMaker";
this.Size = new System.Drawing.Size(250, 30);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button openButton;
private System.Windows.Forms.Button playButton;
private System.Windows.Forms.Button stopButton;
private System.Windows.Forms.OpenFileDialog openFileDialog;
protected virtual void OnPlayStart(EventArgs e)
{
if (PlayStart != null)
{
PlayStart(this, e);
}
}
protected virtual void OnPlayStop(EventArgs e)
{
if (PlayStop != null)
{
PlayStop(this, e);
}
}
SoundPlayer soundPlayer;
public TinyNoiseMaker()
{
InitializeComponent();
soundPlayer = new SoundPlayer();
}
private void openButton_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog.FileName;
}
}
private void playButton_Click(object sender, EventArgs e)
{
Play();
}
private void stopButton_Click(object sender, EventArgs e)
{
Stop();
}
public void Play()
{
soundPlayer.Play();
OnPlayStart(EventArgs.Empty);
}
public void Stop()
{
soundPlayer.Stop();
OnPlayStop(EventArgs.Empty);
}
private string fileName;
public string FileName
{
get
{
return fileName;
}
set
{
if (fileName != value)
{
fileName = value;
soundPlayer.Stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
}
}
}