这不是重复的! - 看完评论后,也许就是了。SetConsoleMode失败,为零,lasterror = 0
我一直在寻找一种方式,以斜体在控制台应用程序的控制台输出文本,在C#,Visual Studio的2015年,瞄准的.NET Framework 4.5.2,OS = Windows 7的
微软文档是很清楚
这是here - 它是如此的误导,这是错误的。这是一个操作系统问题。
我发现与做什么,我想弗拉基米尔尼科夫解决以下问题,
adding text decorations to console output
在其中一个答案回答3月28日在19:52和代码像它在Git中,和其他地方......我的问题是 - 自然 - 它不适合我。
我复制作者的轻微MODS的代码为以下控制台应用程序
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
const int STD_OUTPUT_HANDLE = -11;
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
static void Main()
{
var handle = GetStdHandle(STD_OUTPUT_HANDLE);
uint mode;
GetConsoleMode(handle, out mode);
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
const string UNDERLINE = "\x1B[4m";
const string RESET = "\x1B[0m";
Console.WriteLine("Some " + UNDERLINE + "underlined" + RESET + " text");
Console.ReadLine();
}
}
}
和我得到的VT命令窗口,而不是下划线,作为文章。
我被困从ConsoleSetMode的返回值 - 这是零。我已经看到这个失败与lasterror = 6,但这里lasterror是0.
认为这是最近更新? ... 或者其他的东西?这是一个Windows版本问题 - 显然,Windows 10 AU是必需的。
这只适用于W8或更高版本。 – Gusman
https://stackoverflow.com/questions/38772468/setconsolemode-and-enable-virtual-terminal-processing – Gusman
@Gusman在Windows 8.1上不适用于我。你的意思是Windows 10? – GSerg