重构
This commit is contained in:
parent
6b5d644bd9
commit
1d60a09987
@ -1,11 +1,33 @@
|
|||||||
namespace Sunny.UI
|
namespace Sunny.UI
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 提供程序集引用的常量定义。
|
||||||
|
/// </summary>
|
||||||
internal static class AssemblyRefEx
|
internal static class AssemblyRefEx
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 程序集版本号。
|
||||||
|
/// </summary>
|
||||||
internal const string Version = "4.0.0.0";
|
internal const string Version = "4.0.0.0";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Microsoft 公钥。
|
||||||
|
/// </summary>
|
||||||
internal const string MicrosoftPublicKey = "b03f5f7f11d50a3a";
|
internal const string MicrosoftPublicKey = "b03f5f7f11d50a3a";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// System.Design 程序集引用。
|
||||||
|
/// </summary>
|
||||||
internal const string SystemDesign = "System.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
internal const string SystemDesign = "System.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// System.Drawing.Design 程序集引用。
|
||||||
|
/// </summary>
|
||||||
internal const string SystemDrawingDesign = "System.Drawing.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
internal const string SystemDrawingDesign = "System.Drawing.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// System.Drawing 程序集引用。
|
||||||
|
/// </summary>
|
||||||
internal const string SystemDrawing = "System.Drawing, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
internal const string SystemDrawing = "System.Drawing, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,10 +57,14 @@ namespace Sunny.UI
|
|||||||
BackgroundLoop
|
BackgroundLoop
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SoundPlayer _SoundPlayer;
|
private static SoundPlayer _soundPlayer;
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止播放声音。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sound">SoundPlayer 对象。</param>
|
||||||
private static void InternalStop(SoundPlayer sound)
|
private static void InternalStop(SoundPlayer sound)
|
||||||
{
|
{
|
||||||
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
|
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
|
||||||
@ -74,60 +78,66 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>播放。wav声音文件。</summary>
|
/// <summary>
|
||||||
/// <param name="location">String,包含声音文件的名称 </param>
|
/// 播放 .wav 声音文件。
|
||||||
/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlThread" /><IPermission class="System.Net.WebPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /></PermissionSet>
|
/// </summary>
|
||||||
|
/// <param name="location">包含声音文件的名称的字符串。</param>
|
||||||
public static void Play(string location)
|
public static void Play(string location)
|
||||||
{
|
{
|
||||||
Play(location, AudioPlayMode.Background);
|
Play(location, AudioPlayMode.Background);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 播放。wav声音文件.
|
/// 播放 .wav 声音文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="location">AudioPlayMode枚举模式播放声音。默认情况下,AudioPlayMode.Background。</param>
|
/// <param name="location">包含声音文件的名称的字符串。</param>
|
||||||
/// <param name="playMode">String,包含声音文件的名称</param>
|
/// <param name="playMode">AudioPlayMode 枚举,指示播放模式。</param>
|
||||||
public static void Play(string location, AudioPlayMode playMode)
|
public static void Play(string location, AudioPlayMode playMode)
|
||||||
{
|
{
|
||||||
ValidateAudioPlayModeEnum(playMode, "playMode");
|
ValidateAudioPlayModeEnum(playMode, nameof(playMode));
|
||||||
string text1 = ValidateFilename(location);
|
var validatedLocation = ValidateFilename(location);
|
||||||
SoundPlayer player1 = new SoundPlayer(text1);
|
SoundPlayer player = new SoundPlayer(validatedLocation);
|
||||||
Play(player1, playMode);
|
Play(player, playMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 根据播放模式播放声音。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sound">SoundPlayer 对象。</param>
|
||||||
|
/// <param name="mode">AudioPlayMode 枚举,指示播放模式。</param>
|
||||||
private static void Play(SoundPlayer sound, AudioPlayMode mode)
|
private static void Play(SoundPlayer sound, AudioPlayMode mode)
|
||||||
{
|
{
|
||||||
if (_SoundPlayer != null)
|
if (_soundPlayer != null)
|
||||||
{
|
{
|
||||||
InternalStop(_SoundPlayer);
|
InternalStop(_soundPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
_SoundPlayer = sound;
|
_soundPlayer = sound;
|
||||||
switch (mode)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case AudioPlayMode.WaitToComplete:
|
case AudioPlayMode.WaitToComplete:
|
||||||
_SoundPlayer.PlaySync();
|
_soundPlayer.PlaySync();
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case AudioPlayMode.Background:
|
case AudioPlayMode.Background:
|
||||||
_SoundPlayer.Play();
|
_soundPlayer.Play();
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case AudioPlayMode.BackgroundLoop:
|
case AudioPlayMode.BackgroundLoop:
|
||||||
_SoundPlayer.PlayLooping();
|
_soundPlayer.PlayLooping();
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 播放系统声音。
|
/// 播放系统声音。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="systemSound">对象代表系统播放声音。</param>
|
/// <param name="systemSound">SystemSound 对象,代表系统播放声音。</param>
|
||||||
public static void PlaySystemSound(SystemSound systemSound)
|
public static void PlaySystemSound(SystemSound systemSound)
|
||||||
{
|
{
|
||||||
if (systemSound == null)
|
if (systemSound == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException();
|
throw new ArgumentNullException(nameof(systemSound));
|
||||||
}
|
}
|
||||||
|
|
||||||
systemSound.Play();
|
systemSound.Play();
|
||||||
@ -136,26 +146,35 @@ namespace Sunny.UI
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 停止在后台播放声音。
|
/// 停止在后台播放声音。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <filterpriority>1</filterpriority>
|
|
||||||
public static void Stop()
|
public static void Stop()
|
||||||
{
|
{
|
||||||
SoundPlayer player1 = new SoundPlayer();
|
SoundPlayer player = new SoundPlayer();
|
||||||
InternalStop(player1);
|
InternalStop(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证 AudioPlayMode 枚举值。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">AudioPlayMode 枚举值。</param>
|
||||||
|
/// <param name="paramName">参数名称。</param>
|
||||||
private static void ValidateAudioPlayModeEnum(AudioPlayMode value, string paramName)
|
private static void ValidateAudioPlayModeEnum(AudioPlayMode value, string paramName)
|
||||||
{
|
{
|
||||||
if ((value < AudioPlayMode.WaitToComplete) || (value > AudioPlayMode.BackgroundLoop))
|
if (!Enum.IsDefined(typeof(AudioPlayMode), value))
|
||||||
{
|
{
|
||||||
throw new InvalidEnumArgumentException(paramName, (int)value, typeof(AudioPlayMode));
|
throw new InvalidEnumArgumentException(paramName, (int)value, typeof(AudioPlayMode));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证文件名。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="location">文件名字符串。</param>
|
||||||
|
/// <returns>验证后的文件名。</returns>
|
||||||
private static string ValidateFilename(string location)
|
private static string ValidateFilename(string location)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(location))
|
if (string.IsNullOrEmpty(location))
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException();
|
throw new ArgumentNullException(nameof(location));
|
||||||
}
|
}
|
||||||
|
|
||||||
return location;
|
return location;
|
||||||
@ -170,18 +189,18 @@ namespace Sunny.UI
|
|||||||
public static class Mp3Player
|
public static class Mp3Player
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 播放
|
/// 播放 MP3 文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="MP3_FileName">文件名</param>
|
/// <param name="mp3FileName">文件名。</param>
|
||||||
/// <param name="Repeat">重复</param>
|
/// <param name="repeat">是否重复播放。</param>
|
||||||
public static void Play(string MP3_FileName, bool Repeat)
|
public static void Play(string mp3FileName, bool repeat)
|
||||||
{
|
{
|
||||||
Win32.WinMM.mciSendString("open \"" + MP3_FileName + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
|
Win32.WinMM.mciSendString($"open \"{mp3FileName}\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
|
||||||
Win32.WinMM.mciSendString("play MediaFile" + (Repeat ? " repeat" : string.Empty), null, 0, IntPtr.Zero);
|
Win32.WinMM.mciSendString($"play MediaFile{(repeat ? " repeat" : string.Empty)}", null, 0, IntPtr.Zero);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 暂停
|
/// 暂停播放。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Pause()
|
public static void Pause()
|
||||||
{
|
{
|
||||||
@ -189,7 +208,7 @@ namespace Sunny.UI
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 停止
|
/// 停止播放。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Stop()
|
public static void Stop()
|
||||||
{
|
{
|
||||||
|
@ -153,20 +153,16 @@ namespace Sunny.UI
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class BmpFile
|
public class BmpFile
|
||||||
{
|
{
|
||||||
BmpHead head;
|
|
||||||
|
|
||||||
byte[] data;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 慢于 bitmap.Save(XX,ImageFormat.Bmp),只是为了解释BMP文件数据格式
|
/// 慢于 bitmap.Save(XX,ImageFormat.Bmp),只是为了解释BMP文件数据格式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bitmap"></param>
|
/// <param name="bitmap"></param>
|
||||||
public BmpFile(Bitmap bitmap)
|
public BmpFile(Bitmap bitmap)
|
||||||
{
|
{
|
||||||
head = new BmpHead();
|
var head = new BmpHead();
|
||||||
head.Init(bitmap);
|
head.Init(bitmap);
|
||||||
data = new byte[head.FileSize];
|
Data = new byte[head.FileSize];
|
||||||
Array.Copy(head.ToBytes(), 0, data, 0, (int)head.BitmapDataOffset);
|
Array.Copy(head.ToBytes(), 0, Data, 0, (int)head.BitmapDataOffset);
|
||||||
|
|
||||||
var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
|
var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
|
||||||
var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
|
var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
|
||||||
@ -200,10 +196,10 @@ namespace Sunny.UI
|
|||||||
/// <param name="bmpData">数据,从左上角开始,为每个点的B、G、R循环</param>
|
/// <param name="bmpData">数据,从左上角开始,为每个点的B、G、R循环</param>
|
||||||
public BmpFile(int width, int height, byte[] bmpData)
|
public BmpFile(int width, int height, byte[] bmpData)
|
||||||
{
|
{
|
||||||
head = new BmpHead();
|
var head = new BmpHead();
|
||||||
head.Init(width, height);
|
head.Init(width, height);
|
||||||
data = new byte[head.FileSize];
|
Data = new byte[head.FileSize];
|
||||||
Array.Copy(head.ToBytes(), 0, data, 0, (int)head.BitmapDataOffset);
|
Array.Copy(head.ToBytes(), 0, Data, 0, (int)head.BitmapDataOffset);
|
||||||
if (bmpData.Length != width * height * 3) return;
|
if (bmpData.Length != width * height * 3) return;
|
||||||
|
|
||||||
//BMP文件的数据从左下角开始,每行向上。System.Drawing.Bitmap数据是从左上角开始,每行向下
|
//BMP文件的数据从左下角开始,每行向上。System.Drawing.Bitmap数据是从左上角开始,每行向下
|
||||||
@ -212,7 +208,7 @@ namespace Sunny.UI
|
|||||||
{
|
{
|
||||||
int offset = height - 1 - i;
|
int offset = height - 1 - i;
|
||||||
offset *= width * 3;
|
offset *= width * 3;
|
||||||
Array.Copy(bmpData, offset, data, idx, width * 3);
|
Array.Copy(bmpData, offset, Data, idx, width * 3);
|
||||||
idx += width * 3;
|
idx += width * 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -220,7 +216,7 @@ namespace Sunny.UI
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 二进制数据
|
/// 二进制数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] Data => data;
|
public byte[] Data { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存文件
|
/// 保存文件
|
||||||
@ -228,7 +224,7 @@ namespace Sunny.UI
|
|||||||
/// <param name="fileName">文件名</param>
|
/// <param name="fileName">文件名</param>
|
||||||
public void SaveToFile(string fileName)
|
public void SaveToFile(string fileName)
|
||||||
{
|
{
|
||||||
File.WriteAllBytes(fileName, data);
|
File.WriteAllBytes(fileName, Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -237,7 +233,7 @@ namespace Sunny.UI
|
|||||||
/// <returns>图片</returns>
|
/// <returns>图片</returns>
|
||||||
public Bitmap Bitmap()
|
public Bitmap Bitmap()
|
||||||
{
|
{
|
||||||
MemoryStream ms = new MemoryStream(data);
|
var ms = new MemoryStream(Data);
|
||||||
ms.Position = 0;
|
ms.Position = 0;
|
||||||
return new Bitmap(ms);
|
return new Bitmap(ms);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user