From f2719bb41d9dab65aee7d965c47bb833f1977d01 Mon Sep 17 00:00:00 2001 From: muqing <1966944300@qq.com> Date: Sun, 12 Jan 2025 17:21:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E6=A8=A1=E5=BC=8F=EF=BC=8C=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=96=B9=E6=B3=95=E5=B9=B6=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ModelingMusic/Music.cs | 7 +++-- ModelingMusic/MusicFrom.cs | 49 +++++++++++++++++++++++++++++----- ModelingMusic/MusicFromTest.cs | 0 Program.cs | 14 ++-------- 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 ModelingMusic/MusicFromTest.cs diff --git a/ModelingMusic/Music.cs b/ModelingMusic/Music.cs index 78ff82c..a667ba9 100644 --- a/ModelingMusic/Music.cs +++ b/ModelingMusic/Music.cs @@ -28,7 +28,7 @@ public class Music { Console.CursorVisible = false; // 隐藏光标 Console.Clear(); - Program.printf("加载资源中"); + Console.Write("加载资源中"); // Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小 // Console.SetBufferSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置缓冲区大小 // 获取控制台的大小 @@ -36,11 +36,10 @@ public class Music // consoleHeight = Console.WindowHeight - 1; for (int i = 0; i < 3; i++) { - Program.printf("·"); + Console.Write("·"); await Task.Delay(300); } - Console.WriteLine(); - Program.printfLine("加载完成"); + Console.WriteLine("加载完成"); await Task.Delay(500); Console.Clear(); diff --git a/ModelingMusic/MusicFrom.cs b/ModelingMusic/MusicFrom.cs index d0945e0..cfa47a7 100644 --- a/ModelingMusic/MusicFrom.cs +++ b/ModelingMusic/MusicFrom.cs @@ -11,12 +11,51 @@ namespace Debug.ModelingMusic; public class MusicFrom { + + // 导入kernel32.dll中的SetConsoleMode函数 + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); + + // 导入kernel32.dll中的GetStdHandle函数 + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr GetStdHandle(int nStdHandle); + + // 定义控制台模式常量 + private const uint ENABLE_ECHO_INPUT = 0x0004; + private const uint ENABLE_LINE_INPUT = 0x0002; + private const uint ENABLE_PROCESSED_INPUT = 0x0001; + + // 导入kernel32.dll中的GetConsoleMode函数 + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); + + // 定义标准输入句柄 + private const int STD_INPUT_HANDLE = -10; + private long time = 0; int MaxX = 0; int MaxY = 0; public MusicFrom() { + // 获取标准输入句柄 + IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE); + + // 获取当前控制台输入模式 + uint mode; + if (!GetConsoleMode(hStdin, out mode)) + { + Console.WriteLine("无法获取控制台输入模式"); + return; + } + + // 禁用回显和行输入 + mode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); + if (!SetConsoleMode(hStdin, mode)) + { + Console.WriteLine("无法设置控制台输入模式"); + return; + } setCharArry(); MaxY = Program.chars.GetLength(0); MaxX = Program.chars.GetLength(1); @@ -200,7 +239,7 @@ public class MusicFrom } //分数long 000000000 private long score = 0; - bool[] isDown = new bool[7]; + readonly bool[] isDown = new bool[7]; // 导入user32.dll中的GetAsyncKeyState函数 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern short GetAsyncKeyState(int vkey); @@ -236,7 +275,7 @@ public class MusicFrom while (true) { - + // ConsoleKeyInfo keyInfo = Console.ReadKey(true); // true表示不显示输入的字符 // 检查并处理所有监控的按键状态变化 foreach (var kvp in keyStates) { @@ -262,7 +301,7 @@ public class MusicFrom keyStates[vkey] = isKeyPressed; } // 避免CPU占用过高 - Thread.Sleep(10); + Thread.Sleep(50); } } @@ -347,8 +386,6 @@ public class MusicFrom score += 100; } break; - default: - break; } } @@ -384,8 +421,6 @@ public class MusicFrom isDown[6] = false; // 处理L键被释放的逻辑 break; - default: - break; } } } \ No newline at end of file diff --git a/ModelingMusic/MusicFromTest.cs b/ModelingMusic/MusicFromTest.cs new file mode 100644 index 0000000..e69de29 diff --git a/Program.cs b/Program.cs index bad1c4b..e29f603 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Runtime.InteropServices; +using System.Text; using Debug.ModelingMusic; namespace Debug; /// @@ -9,14 +10,6 @@ namespace Debug; public class Program { - public static void printf(object a) - { - Console.Write(a); - } - public static void printfLine(object a) - { - Console.WriteLine(a); - } public static void Main(string[] args) { Console.Title = "Tools";//设置窗口标题 @@ -63,9 +56,6 @@ public class Program case 5: _ = new Tanchishe.Main(); break; - default: - printfLine("还没有这个功能"); - break; } }, "作者:MuQing \n使用语言:C# \n使用IDE:VS Code \n使用框架:.NET 8.0 \n项目地址:https://git.coldmint.top/muqing/ModelingMusicForm.git"); // modeSwitch.SetMessage(";");