?? media.cs
字號:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
namespace Chapter14.Media
{
public sealed class SoundPlayer : Component
{
//path to the file
private string soundLocation = "";
public SoundPlayer() { }
public SoundPlayer(string soundLocation)
{
//set the path
this.soundLocation = soundLocation;
}
public string SoundLocation
{
get
{
return soundLocation;
}
set
{
if (File.Exists(value))
{
soundLocation = value;
}
OnSoundLocationChanged(EventArgs.Empty);
}
}
public event EventHandler SoundLocationChanged;
private void OnSoundLocationChanged(EventArgs e)
{
if (this.SoundLocationChanged != null)
{
this.SoundLocationChanged(this, e);
}
}
public void Play()
{
//play async
NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.ASYNC);
}
public void PlaySync()
{
//play sync
NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.SYNC);
}
public void PlayLooping()
{
//play looping
NativeMethods.PlaySound(soundLocation, IntPtr.Zero, NativeMethods.SND.FILENAME | NativeMethods.SND.ASYNC | NativeMethods.SND.LOOP);
}
public void Stop()
{
NativeMethods.PlaySound(null, IntPtr.Zero, NativeMethods.SND.NODEFAULT);
}
}
public static class SystemSounds
{
public static SystemSound Beep
{
get
{
return new SystemSound(0);
}
}
public static SystemSound Asterisk
{
get
{
return new SystemSound(NativeMethods.MB.ICONASTERISK);
}
}
public static SystemSound Exclamation
{
get
{
return new SystemSound(NativeMethods.MB.ICONEXCLAMATION);
}
}
public static SystemSound Question
{
get
{
return new SystemSound(NativeMethods.MB.ICONQUESTION);
}
}
public static SystemSound Hand
{
get
{
return new SystemSound(NativeMethods.MB.ICONHAND);
}
}
}
public sealed class SystemSound
{
//type of sound
private NativeMethods.MB soundType;
internal SystemSound(NativeMethods.MB soundType)
{
//set type
this.soundType = soundType;
}
public void Play()
{
//play
NativeMethods.MessageBeep(soundType);
}
}
internal static class NativeMethods
{
[DllImport("coredll.dll")]
internal static extern void MessageBeep(MB type);
internal enum MB
{
ICONHAND = 0x00000010,
ICONQUESTION = 0x00000020,
ICONEXCLAMATION = 0x00000030,
ICONASTERISK = 0x00000040,
}
[DllImport("coredll.dll", EntryPoint = "PlaySoundW")]
[return:MarshalAs(UnmanagedType.Bool)]
internal static extern bool PlaySound(string lpszName, IntPtr hModule, SND dwFlags);
[Flags()]
internal enum SND
{
//ALIAS = 0x00010000,
FILENAME = 0x00020000,
//RESOURCE = 0x00040004,
SYNC = 0x00000000,
ASYNC = 0x00000001,
NODEFAULT = 0x00000002,
//MEMORY = 0x00000004,
LOOP = 0x00000008,
NOSTOP = 0x00000010,
NOWAIT = 0x00002000
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -