I realized this by looking at the Vim source code the corresponding bits can be found in os_win32.c in the mch_init function I copied and pasted the corresponding bit here.
if (read_cmd_fd == 0) g_hConIn = GetStdHandle(STD_INPUT_HANDLE); else create_conin(); g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE); #ifdef FEAT_RESTORE_ORIG_SCREEN SaveConsoleBuffer(&g_cbOrig); g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes; #else GetConsoleScreenBufferInfo(g_hConOut, &csbi); g_attrCurrent = g_attrDefault = csbi.wAttributes; #endif if (cterm_normal_fg_color == 0) cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1; if (cterm_normal_bg_color == 0) cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1; update_tcap(g_attrCurrent); GetConsoleCursorInfo(g_hConOut, &g_cci); GetConsoleMode(g_hConIn, &g_cmodein); GetConsoleMode(g_hConOut, &g_cmodeout); #ifdef FEAT_TITLE SaveConsoleTitleAndIcon(); if (g_fCanChangeIcon) SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon); #endif
This way, it just saves the console information (including the title and icon), and then restores it again when you exit.
Unfortunately, the Console class does not provide access to the contents of the screen buffer, so for this you need P / Invoke in the corresponding Win32 functions.
As an alternative, the Win32 console actually supports several screen buffers , which can be an easier way to implement this - instead of copying an existing screen buffer, just create a new one with CreateConsoleScreenBuffer and set this buffer as the current one shown with SetConsoleActiveScreenBuffer . Again, the Console class, unfortunately, does not support multiple screen buffers, so for this you will need P / Invoke. Also, the Console class always writes to the screen buffer that was active at the time the application was launched, so if you replace the active screen buffer, the Console class will still write to the old screen buffer (which is not a longer view) - to get around this, you you will need to P / call all access to the console, see Working with console screen buffers in .NET .
source share