我设法找到了答案,以防万一有人感兴趣,我已经包含了下面的代码。它很乱,因为我还没有清理它,但它应该作为如何做到这一点的一个例子。
感谢pinvoke.net提供的出色签名集。
[DllImport("kernel32", SetLastError = true)]
static extern bool AllocConsole();
[DllImport("kernel32", SetLastError = true)]
static extern bool FreeConsole();
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32", SetLastError = true)]
static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput,
[Out]StringBuilder lpCharacter, uint nLength, COORD dwReadCoord,
out uint lpNumberOfCharsRead);
const int STD_OUTPUT_HANDLE = -11;
[StructLayout(LayoutKind.Sequential)]
struct COORD
{
public short X;
public short Y;
}
[Test]
public void WriteTitle()
{
bool consoleAllocated = AllocConsole();
if (!consoleAllocated)
throw new Exception("Unable to allocate a new console.");
Curses.InitScr();
Stdscr.Add(4, 6, "This is a test title");
Stdscr.Refresh();
IntPtr stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
uint length = 20;
StringBuilder consoleOutput = new StringBuilder((int)length);
COORD readCoord;
readCoord.X = 6;
readCoord.Y = 4;
uint numOfCharsRead = 0;
ReadConsoleOutputCharacter(stdOut, consoleOutput, length, readCoord, out numOfCharsRead);
string outputString = consoleOutput.ToString();
Assert.That(outputString, Is.EqualTo("This is a test title"));
Curses.EndWin();
FreeConsole();
}