优化控制台输入模式,简化输出方法并调整按键处理逻辑
This commit is contained in:
parent
ec850cd1d6
commit
f2719bb41d
|
@ -28,7 +28,7 @@ public class Music
|
||||||
{
|
{
|
||||||
Console.CursorVisible = false; // 隐藏光标
|
Console.CursorVisible = false; // 隐藏光标
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
Program.printf("加载资源中");
|
Console.Write("加载资源中");
|
||||||
// Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小
|
// Console.SetWindowSize(chars.GetLength(0) * 5, chars.GetLength(1) * 5); // 设置窗口大小
|
||||||
// Console.SetBufferSize(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;
|
// consoleHeight = Console.WindowHeight - 1;
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
Program.printf("·");
|
Console.Write("·");
|
||||||
await Task.Delay(300);
|
await Task.Delay(300);
|
||||||
}
|
}
|
||||||
Console.WriteLine();
|
Console.WriteLine("加载完成");
|
||||||
Program.printfLine("加载完成");
|
|
||||||
await Task.Delay(500);
|
await Task.Delay(500);
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,51 @@ namespace Debug.ModelingMusic;
|
||||||
|
|
||||||
public class MusicFrom
|
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;
|
private long time = 0;
|
||||||
int MaxX = 0;
|
int MaxX = 0;
|
||||||
int MaxY = 0;
|
int MaxY = 0;
|
||||||
|
|
||||||
public MusicFrom()
|
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();
|
setCharArry();
|
||||||
MaxY = Program.chars.GetLength(0);
|
MaxY = Program.chars.GetLength(0);
|
||||||
MaxX = Program.chars.GetLength(1);
|
MaxX = Program.chars.GetLength(1);
|
||||||
|
@ -200,7 +239,7 @@ public class MusicFrom
|
||||||
}
|
}
|
||||||
//分数long 000000000
|
//分数long 000000000
|
||||||
private long score = 0;
|
private long score = 0;
|
||||||
bool[] isDown = new bool[7];
|
readonly bool[] isDown = new bool[7];
|
||||||
// 导入user32.dll中的GetAsyncKeyState函数
|
// 导入user32.dll中的GetAsyncKeyState函数
|
||||||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
|
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
|
||||||
public static extern short GetAsyncKeyState(int vkey);
|
public static extern short GetAsyncKeyState(int vkey);
|
||||||
|
@ -236,7 +275,7 @@ public class MusicFrom
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
// ConsoleKeyInfo keyInfo = Console.ReadKey(true); // true表示不显示输入的字符
|
||||||
// 检查并处理所有监控的按键状态变化
|
// 检查并处理所有监控的按键状态变化
|
||||||
foreach (var kvp in keyStates)
|
foreach (var kvp in keyStates)
|
||||||
{
|
{
|
||||||
|
@ -262,7 +301,7 @@ public class MusicFrom
|
||||||
keyStates[vkey] = isKeyPressed;
|
keyStates[vkey] = isKeyPressed;
|
||||||
}
|
}
|
||||||
// 避免CPU占用过高
|
// 避免CPU占用过高
|
||||||
Thread.Sleep(10);
|
Thread.Sleep(50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,8 +386,6 @@ public class MusicFrom
|
||||||
score += 100;
|
score += 100;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,8 +421,6 @@ public class MusicFrom
|
||||||
isDown[6] = false;
|
isDown[6] = false;
|
||||||
// 处理L键被释放的逻辑
|
// 处理L键被释放的逻辑
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
0
ModelingMusic/MusicFromTest.cs
Normal file
0
ModelingMusic/MusicFromTest.cs
Normal file
14
Program.cs
14
Program.cs
|
@ -1,4 +1,5 @@
|
||||||
using System.Text;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
using Debug.ModelingMusic;
|
using Debug.ModelingMusic;
|
||||||
namespace Debug;
|
namespace Debug;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -9,14 +10,6 @@ namespace Debug;
|
||||||
public class Program
|
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)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.Title = "Tools";//设置窗口标题
|
Console.Title = "Tools";//设置窗口标题
|
||||||
|
@ -63,9 +56,6 @@ public class Program
|
||||||
case 5:
|
case 5:
|
||||||
_ = new Tanchishe.Main();
|
_ = new Tanchishe.Main();
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
printfLine("还没有这个功能");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}, "作者:MuQing \n使用语言:C# \n使用IDE:VS Code \n使用框架:.NET 8.0 \n项目地址:https://git.coldmint.top/muqing/ModelingMusicForm.git");
|
}, "作者:MuQing \n使用语言:C# \n使用IDE:VS Code \n使用框架:.NET 8.0 \n项目地址:https://git.coldmint.top/muqing/ModelingMusicForm.git");
|
||||||
// modeSwitch.SetMessage(";");
|
// modeSwitch.SetMessage(";");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user