📄 win32interop.cs
字号:
using System;
using System.Runtime.InteropServices;
namespace ICSharpCode.ConsolePad
{
public class Win32Interop
{
public const int STD_OUTPUT_HANDLE = -11;
public const int STD_INPUT_HANDLE = -10;
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public const int NORMAL_PRIORITY_CLASS = 0x00000020;
public const int SW_HIDE = 0;
public const int WM_SYSKEYDOWN = 0x0104;
public const int WM_SYSKEYUP = 0x0105;
public const int KEY_EVENT = 0x0001;
[DllImport("kernel32.dll")]
public static extern bool WriteConsole(IntPtr hConsoleOutput, String lpBuffer, Int32 nNumberOfCharsToWrite, out Int32 lpNumberOfCharsWritten, IntPtr lpReserved);
[DllImport("kernel32.dll",CharSet=CharSet.Unicode)]
public static extern bool WriteConsoleInput(IntPtr hIn, [MarshalAs(UnmanagedType.LPStruct)]KEY_INPUT_RECORD r, Int32 count, out Int32 countOut);
[DllImport("kernel32.dll")]
public static extern bool SetConsoleCursorPosition(IntPtr hOut, COORD newPos);
[DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern int GetConsoleCursorInfo(IntPtr hConsoleOutput, out CONSOLE_CURSOR_INFO lpConsoleCursorInfo);
[DllImport("kernel32.dll")]
public static extern bool ReadConsoleOutput(IntPtr hConsoleOutput, [Out]CHAR_INFO[] lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool SetConsoleTitle(string lpConsoleTitle);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("kernel32.dll")]
public static extern int SetConsoleWindowInfo(IntPtr hConsoleOutput, bool bAbsolute, [In] ref SMALL_RECT lpConsoleWindow);
[DllImport("kernel32.dll")]
public static extern bool SetConsoleScreenBufferSize(IntPtr hConsoleOutput, COORD dwSize);
[DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
public static extern bool FreeConsole();
[DllImport("kernel32.dll")]
public static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation);
[DllImport("user32.dll")]
public static extern bool TranslateMessage(ref System.Windows.Forms.Message msgValue);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("Kernel32")]
public static extern Boolean SetConsoleCtrlHandler(HandlerRoutine Handler, Boolean Add);
[DllImport ("kernel32.dll", EntryPoint="GetLargestConsoleWindowSize", SetLastError=true, CharSet=CharSet.Unicode)]
public extern static COORD GetLargestConsoleWindowSize(IntPtr handle);
public delegate Boolean HandlerRoutine(CtrlTypes CtrlType);
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public class KEY_INPUT_RECORD
{
public Int16 EventType = KEY_EVENT;
public bool bKeyDown;
public Int16 wRepeatCount;
public Int16 wVirtualKeyCode;
public Int16 wVirtualScanCode;
public char UnicodeChar;
public Int32 dwControlKeyState;
}
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public int hwnd;
public int message;
public int wParam;
public int lParam;
public int itime;
public int pt_x;
public int pt_y;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[StructLayout(LayoutKind.Explicit, CharSet =CharSet.Auto)]
public struct CHAR_TYPE
{
[FieldOffset(0)]
public short UnicodeChar;
[FieldOffset(0)]
public byte AsciiChar;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct CHAR_INFO
{
public CHAR_TYPE Character;
public short Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_CURSOR_INFO
{
public int dwSize;
public bool bVisible;
}
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short x;
public short y;
}
[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -