From 1d60a099870e132a75164936ca5d5ea874c30d9e Mon Sep 17 00:00:00 2001 From: Sunny Date: Fri, 27 Dec 2024 21:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Common/UAssemblyRef.cs | 22 +++++++++ SunnyUI/Common/UAudio.cs | 89 +++++++++++++++++++++------------- SunnyUI/Common/UBmp.cs | 24 ++++----- 3 files changed, 86 insertions(+), 49 deletions(-) diff --git a/SunnyUI/Common/UAssemblyRef.cs b/SunnyUI/Common/UAssemblyRef.cs index 75d7a246..f21d27f1 100644 --- a/SunnyUI/Common/UAssemblyRef.cs +++ b/SunnyUI/Common/UAssemblyRef.cs @@ -1,11 +1,33 @@ namespace Sunny.UI { + /// + /// 提供程序集引用的常量定义。 + /// internal static class AssemblyRefEx { + /// + /// 程序集版本号。 + /// internal const string Version = "4.0.0.0"; + + /// + /// Microsoft 公钥。 + /// internal const string MicrosoftPublicKey = "b03f5f7f11d50a3a"; + + /// + /// System.Design 程序集引用。 + /// internal const string SystemDesign = "System.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey; + + /// + /// System.Drawing.Design 程序集引用。 + /// internal const string SystemDrawingDesign = "System.Drawing.Design, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey; + + /// + /// System.Drawing 程序集引用。 + /// internal const string SystemDrawing = "System.Drawing, Version=" + Version + ", Culture=neutral, PublicKeyToken=" + MicrosoftPublicKey; } } diff --git a/SunnyUI/Common/UAudio.cs b/SunnyUI/Common/UAudio.cs index de7572a6..4bc161af 100644 --- a/SunnyUI/Common/UAudio.cs +++ b/SunnyUI/Common/UAudio.cs @@ -57,10 +57,14 @@ namespace Sunny.UI BackgroundLoop } - private static SoundPlayer _SoundPlayer; + private static SoundPlayer _soundPlayer; #region Methods + /// + /// 停止播放声音。 + /// + /// SoundPlayer 对象。 private static void InternalStop(SoundPlayer sound) { new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); @@ -74,60 +78,66 @@ namespace Sunny.UI } } - /// 播放。wav声音文件。 - /// String,包含声音文件的名称 - /// + /// + /// 播放 .wav 声音文件。 + /// + /// 包含声音文件的名称的字符串。 public static void Play(string location) { Play(location, AudioPlayMode.Background); } /// - /// 播放。wav声音文件. + /// 播放 .wav 声音文件。 /// - /// AudioPlayMode枚举模式播放声音。默认情况下,AudioPlayMode.Background。 - /// String,包含声音文件的名称 + /// 包含声音文件的名称的字符串。 + /// AudioPlayMode 枚举,指示播放模式。 public static void Play(string location, AudioPlayMode playMode) { - ValidateAudioPlayModeEnum(playMode, "playMode"); - string text1 = ValidateFilename(location); - SoundPlayer player1 = new SoundPlayer(text1); - Play(player1, playMode); + ValidateAudioPlayModeEnum(playMode, nameof(playMode)); + var validatedLocation = ValidateFilename(location); + SoundPlayer player = new SoundPlayer(validatedLocation); + Play(player, playMode); } + /// + /// 根据播放模式播放声音。 + /// + /// SoundPlayer 对象。 + /// AudioPlayMode 枚举,指示播放模式。 private static void Play(SoundPlayer sound, AudioPlayMode mode) { - if (_SoundPlayer != null) + if (_soundPlayer != null) { - InternalStop(_SoundPlayer); + InternalStop(_soundPlayer); } - _SoundPlayer = sound; + _soundPlayer = sound; switch (mode) { case AudioPlayMode.WaitToComplete: - _SoundPlayer.PlaySync(); - return; + _soundPlayer.PlaySync(); + break; case AudioPlayMode.Background: - _SoundPlayer.Play(); - return; + _soundPlayer.Play(); + break; case AudioPlayMode.BackgroundLoop: - _SoundPlayer.PlayLooping(); - return; + _soundPlayer.PlayLooping(); + break; } } /// /// 播放系统声音。 /// - /// 对象代表系统播放声音。 + /// SystemSound 对象,代表系统播放声音。 public static void PlaySystemSound(SystemSound systemSound) { if (systemSound == null) { - throw new ArgumentNullException(); + throw new ArgumentNullException(nameof(systemSound)); } systemSound.Play(); @@ -136,26 +146,35 @@ namespace Sunny.UI /// /// 停止在后台播放声音。 /// - /// 1 public static void Stop() { - SoundPlayer player1 = new SoundPlayer(); - InternalStop(player1); + SoundPlayer player = new SoundPlayer(); + InternalStop(player); } + /// + /// 验证 AudioPlayMode 枚举值。 + /// + /// AudioPlayMode 枚举值。 + /// 参数名称。 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)); } } + /// + /// 验证文件名。 + /// + /// 文件名字符串。 + /// 验证后的文件名。 private static string ValidateFilename(string location) { if (string.IsNullOrEmpty(location)) { - throw new ArgumentNullException(); + throw new ArgumentNullException(nameof(location)); } return location; @@ -170,18 +189,18 @@ namespace Sunny.UI public static class Mp3Player { /// - /// 播放 + /// 播放 MP3 文件。 /// - /// 文件名 - /// 重复 - 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("play MediaFile" + (Repeat ? " repeat" : string.Empty), 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); } /// - /// 暂停 + /// 暂停播放。 /// public static void Pause() { @@ -189,7 +208,7 @@ namespace Sunny.UI } /// - /// 停止 + /// 停止播放。 /// public static void Stop() { diff --git a/SunnyUI/Common/UBmp.cs b/SunnyUI/Common/UBmp.cs index d11033e4..9a77a4fd 100644 --- a/SunnyUI/Common/UBmp.cs +++ b/SunnyUI/Common/UBmp.cs @@ -153,20 +153,16 @@ namespace Sunny.UI /// public class BmpFile { - BmpHead head; - - byte[] data; - /// /// 慢于 bitmap.Save(XX,ImageFormat.Bmp),只是为了解释BMP文件数据格式 /// /// public BmpFile(Bitmap bitmap) { - head = new BmpHead(); + var head = new BmpHead(); head.Init(bitmap); - data = new byte[head.FileSize]; - Array.Copy(head.ToBytes(), 0, data, 0, (int)head.BitmapDataOffset); + Data = new byte[head.FileSize]; + Array.Copy(head.ToBytes(), 0, Data, 0, (int)head.BitmapDataOffset); var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height); var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb); @@ -200,10 +196,10 @@ namespace Sunny.UI /// 数据,从左上角开始,为每个点的B、G、R循环 public BmpFile(int width, int height, byte[] bmpData) { - head = new BmpHead(); + var head = new BmpHead(); head.Init(width, height); - data = new byte[head.FileSize]; - Array.Copy(head.ToBytes(), 0, data, 0, (int)head.BitmapDataOffset); + Data = new byte[head.FileSize]; + Array.Copy(head.ToBytes(), 0, Data, 0, (int)head.BitmapDataOffset); if (bmpData.Length != width * height * 3) return; //BMP文件的数据从左下角开始,每行向上。System.Drawing.Bitmap数据是从左上角开始,每行向下 @@ -212,7 +208,7 @@ namespace Sunny.UI { int offset = height - 1 - i; offset *= width * 3; - Array.Copy(bmpData, offset, data, idx, width * 3); + Array.Copy(bmpData, offset, Data, idx, width * 3); idx += width * 3; } } @@ -220,7 +216,7 @@ namespace Sunny.UI /// /// 二进制数据 /// - public byte[] Data => data; + public byte[] Data { get; } /// /// 保存文件 @@ -228,7 +224,7 @@ namespace Sunny.UI /// 文件名 public void SaveToFile(string fileName) { - File.WriteAllBytes(fileName, data); + File.WriteAllBytes(fileName, Data); } /// @@ -237,7 +233,7 @@ namespace Sunny.UI /// 图片 public Bitmap Bitmap() { - MemoryStream ms = new MemoryStream(data); + var ms = new MemoryStream(Data); ms.Position = 0; return new Bitmap(ms); }