我需要从控制台中的特定位置读取文本,例如5,5.
如果我需要写到这个位置,它只会是:
Console.SetCursorPosition(5, 5);
Console.Write("My text");
Run Code Online (Sandbox Code Playgroud)
有什么方法可以用类似的方式阅读吗?
只是为了澄清:我不想停止接受用户的输入,即使输入不是来自用户,也不是先前打印出来的东西.我真的想要某种:Console.GetCharAtLocation(5,5)或类似的东西.
Sim*_*ier 13
这是一个C#代码实用程序,可以读取Console缓冲区中当前的内容(不是窗口,缓冲区):
样品用法:
class Program
{
static void Main(string[] args)
{
// read 10 lines from the top of the console buffer
foreach (string line in ConsoleReader.ReadFromBuffer(0, 0, (short)Console.BufferWidth, 10))
{
Console.Write(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
效用:
public class ConsoleReader
{
public static IEnumerable<string> ReadFromBuffer(short x, short y, short width, short height)
{
IntPtr buffer = Marshal.AllocHGlobal(width * height * Marshal.SizeOf(typeof(CHAR_INFO)));
if (buffer == null)
throw new OutOfMemoryException();
try
{
COORD coord = new COORD();
SMALL_RECT rc = new SMALL_RECT();
rc.Left = x;
rc.Top = y;
rc.Right = (short)(x + width - 1);
rc.Bottom = (short)(y + height - 1);
COORD size = new COORD();
size.X = width;
size.Y = height;
const int STD_OUTPUT_HANDLE = -11;
if (!ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE), buffer, size, coord, ref rc))
{
// 'Not enough storage is available to process this command' may be raised for buffer size > 64K (see ReadConsoleOutput doc.)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
IntPtr ptr = buffer;
for (int h = 0; h < height; h++)
{
StringBuilder sb = new StringBuilder();
for (int w = 0; w < width; w++)
{
CHAR_INFO ci = (CHAR_INFO)Marshal.PtrToStructure(ptr, typeof(CHAR_INFO));
char[] chars = Console.OutputEncoding.GetChars(ci.charData);
sb.Append(chars[0]);
ptr += Marshal.SizeOf(typeof(CHAR_INFO));
}
yield return sb.ToString();
}
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
[StructLayout(LayoutKind.Sequential)]
private struct CHAR_INFO
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] charData;
public short attributes;
}
[StructLayout(LayoutKind.Sequential)]
private struct COORD
{
public short X;
public short Y;
}
[StructLayout(LayoutKind.Sequential)]
private struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadConsoleOutput(IntPtr hConsoleOutput, IntPtr lpBuffer, COORD dwBufferSize, COORD dwBufferCoord, ref SMALL_RECT lpReadRegion);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
}
Run Code Online (Sandbox Code Playgroud)
此功能不存在.从理论上讲,您可以覆盖控制台上的输入和输出流,以保留您自己可以读取的控制台缓冲区的副本,但这将是非常重要的(并且可能无法支持所有边缘情况,例如作为一个外部程序挂钩到您的控制台并读/写它).
Windows 10中的简化演示程序,用于从(X, Y)屏幕上的指定位置读取单个字符。在.NET 4.7.2中进行了测试。¹
首先,这是一行代码,其中使用演示网格填充了控制台。请注意,它应该呈现在屏幕的左上角,以便演示可以正常工作。
static void Populate_Console()
{
Console.Clear();
Console.Write(@"
?????????
1?C D E F?
2?G H I J?
3?K L M N?
4?O P Q R?
?????????
2 4 6 8
".TrimStart('\r', '\n'));
}
Run Code Online (Sandbox Code Playgroud)
它看起来应该像这样:
现在,让我们回读一些字符。首先,您需要stdout的本机控制台句柄。这是从Win32获取它的P / Invoke方法:
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr GetStdHandle(int num);
Run Code Online (Sandbox Code Playgroud)
现在是最酷的部分;到目前为止,这似乎是此页面上唯一使用ReadConsoleOutputCharacterWin32函数的答案。尽管它不能让您获得字符颜色属性,但这种方法确实省去了处理复制矩形以及必须CreateConsoleScreenBuffer分配屏幕缓冲区并在它们之间进行复制的所有麻烦。
有单独的Ansi和Unicode版本,您需要根据“控制台”窗口中活动的代码页来调用正确的版本。我在这里显示了两个P / Invoke签名,但是为了简单起见,在示例中,我将继续使用Ansi版本:
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.Bool)] // ????????????????????^
static extern bool ReadConsoleOutputCharacterA(
IntPtr hStdout, // result of 'GetStdHandle(-11)'
out byte ch, // A?N?S?I? character result
uint c_in, // (set to '1')
uint coord_XY, // screen location to read, X:loword, Y:hiword
out uint c_out); // (unwanted, discard)
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)] // ?????????????????????^
static extern bool ReadConsoleOutputCharacterW(
IntPtr hStdout, // result of 'GetStdHandle(-11)'
out Char ch, // U?n?i?c?o?d?e? character result
uint c_in, // (set to '1')
uint coord_XY, // screen location to read, X:loword, Y:hiword
out uint c_out); // (unwanted, discard)
Run Code Online (Sandbox Code Playgroud)
您可能会注意到,出于示例代码的目的,我已将这些代码的编组简化到最低限度,该示例代码旨在一次仅获取一个字符。因此,由于托管指针声明' '和' ' ,您可能会发现c_in必须始终为。1out byte chout Char ch
这实际上就是您所需要的;如果将自己限制为读取单个字符,则如上所述调用适当的P / Invoke函数通常是不言自明的。为了用一个简单的例子展示这一点,我将完成一个可爱的演示程序,该程序从Console绘制的四个字符沿我们在上方绘制的网格的对角线读取四个字符。
static void Windows_Console_Readback()
{
var stdout = GetStdHandle(-11);
for (uint coord, y = 1; y <= 4; y++)
{
coord = (5 - y) * 2; // loword <-- X coord to read
coord |= y << 16; // hiword <-- Y coord to read
if (!ReadConsoleOutputCharacterA(
stdout,
out byte chAnsi, // result: single ANSI char
1, // # of chars to read
coord, // (X,Y) screen location to read (see above)
out _)) // result: actual # of chars (unwanted)
throw new Win32Exception();
Console.Write(" " + (Char)chAnsi + " ");
}
}
Run Code Online (Sandbox Code Playgroud)
那里有...