Design Pattern Examples
Overview of object-oriented design patterns
EnableVTModeForWindowsConsole.cs
Go to the documentation of this file.
1
6
7using System;
8using System.IO;
9using System.Runtime.InteropServices;
10
12{
26 internal class EnableVTModeForWindowsConsole : IDisposable
27 {
28 IntPtr hStdOut;
30 private bool disposedValue;
31
37 {
40 // If we running on Windows then change the mode
41 if (Path.DirectorySeparatorChar == '\\')
42 {
45 {
47 {
49 SetConsoleMode(hStdOut, outConsoleMode);
50 }
51 }
52 }
53 }
54
55 internal const IntPtr INVALID_HANDLE_VALUE = -1;
56 internal const uint INVALID_MODE = 0xffffffff;
57 internal const int STD_OUTPUT_HANDLE = -11;
58 internal const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
59
60 // These should not be called or otherwise referenced if not running
61 // on Windows.
62 [DllImport("kernel32.dll", SetLastError = true)]
63 internal static extern IntPtr GetStdHandle(int nStdHandle);
64
65 [DllImport("kernel32.dll", SetLastError = true)]
66 internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode);
67
68 [DllImport("kernel32.dll", SetLastError = true)]
69 internal static extern bool GetConsoleMode(IntPtr handle, out uint mode);
70
71 protected virtual void Dispose(bool disposing)
72 {
73 if (!disposedValue)
74 {
75 if (disposing)
76 {
78 {
80 }
81 }
82
83 disposedValue = true;
84 }
85 }
86
88 {
89 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
90 Dispose(disposing: false);
91 }
92
93 public void Dispose()
94 {
95 // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
96 Dispose(disposing: true);
97 GC.SuppressFinalize(this);
98 }
99 }
100
101}
102
Enables the virtual terminal processing mode on the current Windows Console. When the program ends,...
static bool SetConsoleMode(IntPtr hConsoleHandle, uint mode)
static bool GetConsoleMode(IntPtr handle, out uint mode)
EnableVTModeForWindowsConsole()
Constructor that enables the virtual terminal processing for the current console output.
The namespace containing all Design Pattern Examples implemented in C#.