4
4
5
5
using System ;
6
6
using System . Diagnostics . CodeAnalysis ;
7
+ using System . Runtime . InteropServices ;
7
8
using System . Text ;
8
9
using System . Windows . Forms ;
9
10
@@ -163,6 +164,47 @@ public static void CaptureScreen(ConsoleKeyInfo? key = null, object arg = null)
163
164
\red255\green255\blue255;
164
165
" ;
165
166
167
+ private static string GetRTFColorFromColorRef ( NativeMethods . COLORREF colorref )
168
+ {
169
+ return string . Concat ( "\\ red" , colorref . R . ToString ( "D" ) ,
170
+ "\\ green" , colorref . G . ToString ( "D" ) ,
171
+ "\\ blue" , colorref . B . ToString ( "D" ) , ";" ) ;
172
+ }
173
+
174
+ private static string GetColorTable ( )
175
+ {
176
+ var handle = NativeMethods . GetStdHandle ( ( uint ) StandardHandleId . Output ) ;
177
+ var csbe = new NativeMethods . CONSOLE_SCREEN_BUFFER_INFO_EX
178
+ {
179
+ cbSize = Marshal . SizeOf ( typeof ( NativeMethods . CONSOLE_SCREEN_BUFFER_INFO_EX ) )
180
+ } ;
181
+ if ( NativeMethods . GetConsoleScreenBufferInfoEx ( handle , ref csbe ) )
182
+ {
183
+ return GetRTFColorFromColorRef ( csbe . Black ) +
184
+ GetRTFColorFromColorRef ( csbe . DarkBlue ) +
185
+ GetRTFColorFromColorRef ( csbe . DarkGreen ) +
186
+ GetRTFColorFromColorRef ( csbe . DarkCyan ) +
187
+ GetRTFColorFromColorRef ( csbe . DarkRed ) +
188
+ GetRTFColorFromColorRef ( csbe . DarkMagenta ) +
189
+ GetRTFColorFromColorRef ( csbe . DarkYellow ) +
190
+ GetRTFColorFromColorRef ( csbe . Gray ) +
191
+ GetRTFColorFromColorRef ( csbe . DarkGray ) +
192
+ GetRTFColorFromColorRef ( csbe . Blue ) +
193
+ GetRTFColorFromColorRef ( csbe . Green ) +
194
+ GetRTFColorFromColorRef ( csbe . Cyan ) +
195
+ GetRTFColorFromColorRef ( csbe . Red ) +
196
+ GetRTFColorFromColorRef ( csbe . Magenta ) +
197
+ GetRTFColorFromColorRef ( csbe . Yellow ) +
198
+ GetRTFColorFromColorRef ( csbe . White ) ;
199
+ }
200
+
201
+ // A bit of a hack if the above failed - assume PowerShell's color scheme if the
202
+ // background color is Magenta, otherwise we assume the default scheme.
203
+ return Console . BackgroundColor == ConsoleColor . DarkMagenta
204
+ ? PowerShellColorTable
205
+ : CmdColorTable ;
206
+ }
207
+
166
208
private static void DumpScreenToClipboard ( int top , int count )
167
209
{
168
210
var buffer = ReadBufferLines ( top , count ) ;
@@ -174,12 +216,7 @@ private static void DumpScreenToClipboard(int top, int count)
174
216
var rtfBuffer = new StringBuilder ( ) ;
175
217
rtfBuffer . Append ( @"{\rtf\ansi{\fonttbl{\f0 Consolas;}}" ) ;
176
218
177
- // A bit of a hack because I don't know how to find the shortcut used to start
178
- // the current console. We assume if the background color is Magenta, then
179
- // PowerShell's color scheme is being used, otherwise we assume the default scheme.
180
- var colorTable = Console . BackgroundColor == ConsoleColor . DarkMagenta
181
- ? PowerShellColorTable
182
- : CmdColorTable ;
219
+ var colorTable = GetColorTable ( ) ;
183
220
rtfBuffer . AppendFormat ( @"{{\colortbl;{0}}}{1}" , colorTable , Environment . NewLine ) ;
184
221
rtfBuffer . Append ( @"\f0 \fs18 " ) ;
185
222
0 commit comments