【优化】在win下自动禁用console的快速编辑模式

main
walon 2021-08-09 12:18:38 +08:00
parent e7066c579b
commit 42d297ac29
3 changed files with 80 additions and 0 deletions

View File

@ -149,6 +149,7 @@ Options:
static void Main(string[] args)
{
ConsoleWindow.EnableQuickEditMode(false);
Console.OutputEncoding = System.Text.Encoding.UTF8;
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var profile = new ProfileTimer();

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Common.Utils
{
using System;
using System.Runtime.InteropServices;
public static class ConsoleWindow
{
private static class NativeFunctions
{
public enum StdHandle : int
{
STD_INPUT_HANDLE = -10,
STD_OUTPUT_HANDLE = -11,
STD_ERROR_HANDLE = -12,
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle); //returns Handle
public enum ConsoleMode : uint
{
ENABLE_ECHO_INPUT = 0x0004,
ENABLE_EXTENDED_FLAGS = 0x0080,
ENABLE_INSERT_MODE = 0x0020,
ENABLE_LINE_INPUT = 0x0002,
ENABLE_MOUSE_INPUT = 0x0010,
ENABLE_PROCESSED_INPUT = 0x0001,
ENABLE_QUICK_EDIT_MODE = 0x0040,
ENABLE_WINDOW_INPUT = 0x0008,
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,
//screen buffer handle
ENABLE_PROCESSED_OUTPUT = 0x0001,
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
ENABLE_LVB_GRID_WORLDWIDE = 0x0010
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
public static void EnableQuickEditMode(bool enable)
{
if (System.OperatingSystem.IsWindows())
{
//QuickEdit lets the user select text in the console window with the mouse, to copy to the windows clipboard.
//But selecting text stops the console process (e.g. unzipping). This may not be always wanted.
IntPtr consoleHandle = NativeFunctions.GetStdHandle((int)NativeFunctions.StdHandle.STD_INPUT_HANDLE);
UInt32 consoleMode;
NativeFunctions.GetConsoleMode(consoleHandle, out consoleMode);
if (enable)
{
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
}
else
{
consoleMode &= ~((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
}
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_EXTENDED_FLAGS);
NativeFunctions.SetConsoleMode(consoleHandle, consoleMode);
}
}
}
}

View File

@ -40,6 +40,7 @@ namespace Luban.Server
static void Main(string[] args)
{
ConsoleWindow.EnableQuickEditMode(false);
Console.OutputEncoding = System.Text.Encoding.UTF8;
var options = ParseOptions(args);