這不是重複的! - 看完評論後,也許就是了。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