// Sets the console font properties. void SetFontProperties(int fontX, int fontY, wchar_t *fontName) { HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); /* Retrieving pointers for Windows Vista/7 Functions */ PGetCurrentConsoleFontEx pGetCurrentConsoleFontEx = (PGetCurrentConsoleFontEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "GetCurrentConsoleFontEx"); PSetCurrentConsoleFontEx pSetCurrentConsoleFontEx = (PSetCurrentConsoleFontEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetCurrentConsoleFontEx"); /* Check for pointers: if they exist, we have Vista/7 and we can use them */ if (pGetCurrentConsoleFontEx && pSetCurrentConsoleFontEx) { CONSOLE_FONT_INFOEX cfi = { sizeof(CONSOLE_FONT_INFOEX) }; /* Tell the font info how big it is, to avoid memory corruption */ cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX); pGetCurrentConsoleFontEx(hOutput, TRUE, &cfi); /* Set the font type to name indicated by wide string literal */ /* Set to 0 to keep current settings */ lstrcpyW(cfi.FaceName, fontName); cfi.dwFontSize.X = fontX; cfi.dwFontSize.Y = fontY; cfi.FontFamily = 0; /* Set to 0x30 for Terminal font */ cfi.FontWeight = 0; /* Set the console font info */ pSetCurrentConsoleFontEx(hOutput, TRUE, &cfi); } else /* We don't have access to the vista functions */ { CONSOLE_INFO2 ci = { sizeof(ci) }; HWND hwndConsole = GetConsoleWindow(); GetConsoleSizeInfo(&ci, hOutput); /* Set to 0 to keep current settings */ ci.FontSize.X = fontX; ci.FontSize.Y = fontY; ci.FontFamily = 0; /* Set to 0x30 for Terminal font */ ci.FontWeight = 0; lstrcpyW(ci.FaceName, fontName); ci.CursorSize = 100; ci.FullScreen = FALSE; ci.QuickEdit = FALSE; ci.AutoPosition = 0x10000; ci.InsertMode = TRUE; ci.ScreenColors = MAKEWORD(0x7, 0x0); ci.PopupColors = MAKEWORD(0x5, 0xf); ci.HistoryNoDup = TRUE; ci.HistoryBufferSize = 50; ci.NumberOfHistoryBuffers = 4; ci.CodePage = 0; ci.Hwnd = hwndConsole; lstrcpyW(ci.ConsoleTitle, L""); SetConsoleInfo(hwndConsole, &ci); } }
void SetConsoleFontSizeTo(HWND inConWnd, int inSizeY, int inSizeX, const wchar_t *asFontName, WORD anTextColors /*= 0*/, WORD anPopupColors /*= 0*/) { _ASSERTE(_WIN32_WINNT_VISTA==0x600); OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)}; DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL); if (VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask)) { // We have Vista apiSetConsoleFontSize(GetStdHandle(STD_OUTPUT_HANDLE), inSizeY, inSizeX, asFontName); } else { // We have older NT (Win2k, WinXP) // So... using undocumented hack const COLORREF DefaultColors[16] = { 0x00000000, 0x00800000, 0x00008000, 0x00808000, 0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0, 0x00808080, 0x00ff0000, 0x0000ff00, 0x00ffff00, 0x000000ff, 0x00ff00ff, 0x0000ffff, 0x00ffffff }; if (!gpConsoleInfoStr) { gpConsoleInfoStr = (CONSOLE_INFO*)LocalAlloc(LPTR, sizeof(CONSOLE_INFO)); if (!gpConsoleInfoStr) { _ASSERTE(gpConsoleInfoStr!=NULL); return; // memory allocation failed } gpConsoleInfoStr->Length = sizeof(CONSOLE_INFO); } if (!anTextColors) { CONSOLE_SCREEN_BUFFER_INFO csbi = {}; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); anTextColors = csbi.wAttributes ? csbi.wAttributes : MAKEWORD(0x7, 0x0); } if (!anPopupColors) { anPopupColors = MAKEWORD(0x5, 0xf); } // get current size/position settings rather than using defaults.. GetConsoleSizeInfo(gpConsoleInfoStr); // set these to zero to keep current settings gpConsoleInfoStr->FontSize.X = inSizeX; gpConsoleInfoStr->FontSize.Y = inSizeY; gpConsoleInfoStr->FontFamily = 0;//0x30;//FF_MODERN|FIXED_PITCH;//0x30; gpConsoleInfoStr->FontWeight = 0;//0x400; lstrcpynW(gpConsoleInfoStr->FaceName, asFontName ? asFontName : L"Lucida Console", countof(gpConsoleInfoStr->FaceName)); //-V303 gpConsoleInfoStr->CursorSize = 25; gpConsoleInfoStr->FullScreen = FALSE; gpConsoleInfoStr->QuickEdit = FALSE; //gpConsoleInfoStr->AutoPosition = 0x10000; gpConsoleInfoStr->AutoPosition = FALSE; RECT rcCon; GetWindowRect(inConWnd, &rcCon); gpConsoleInfoStr->WindowPosX = rcCon.left; gpConsoleInfoStr->WindowPosY = rcCon.top; gpConsoleInfoStr->InsertMode = TRUE; gpConsoleInfoStr->ScreenColors = anTextColors; //MAKEWORD(0x7, 0x0); gpConsoleInfoStr->PopupColors = anPopupColors; //MAKEWORD(0x5, 0xf); gpConsoleInfoStr->HistoryNoDup = FALSE; gpConsoleInfoStr->HistoryBufferSize = 50; gpConsoleInfoStr->NumberOfHistoryBuffers = 32; //-V112 // Issue 700: Default history buffers count too small. HKEY hkConsole = NULL; LONG lRegRc; if (0 == (lRegRc = RegCreateKeyEx(HKEY_CURRENT_USER, L"Console\\ConEmu", 0, NULL, 0, KEY_READ, NULL, &hkConsole, NULL))) { DWORD nSize = sizeof(DWORD), nValue, nType; struct { LPCWSTR pszName; DWORD nMin, nMax; ULONG *pnVal; } BufferValues[] = { {L"HistoryBufferSize", 16, 999, &gpConsoleInfoStr->HistoryBufferSize}, {L"NumberOfHistoryBuffers", 16, 999, &gpConsoleInfoStr->NumberOfHistoryBuffers} }; for (size_t i = 0; i < countof(BufferValues); ++i) { lRegRc = RegQueryValueEx(hkConsole, BufferValues[i].pszName, NULL, &nType, (LPBYTE)&nValue, &nSize); if ((lRegRc == 0) && (nType == REG_DWORD) && (nSize == sizeof(DWORD))) { if (nValue < BufferValues[i].nMin) nValue = BufferValues[i].nMin; else if (nValue > BufferValues[i].nMax) nValue = BufferValues[i].nMax; if (nValue != *BufferValues[i].pnVal) *BufferValues[i].pnVal = nValue; } } } // color table for(size_t i = 0; i < 16; i++) gpConsoleInfoStr->ColorTable[i] = DefaultColors[i]; gpConsoleInfoStr->CodePage = GetConsoleOutputCP();//0;//0x352; gpConsoleInfoStr->Hwnd = inConWnd; gpConsoleInfoStr->ConsoleTitle[0] = 0; // Send data to console window SetConsoleInfo(inConWnd, gpConsoleInfoStr); } }
/****************************************************************************** SetConsolePalette Sets the console palette and font size. ******************************************************************************/ VOID WINAPI SetConsolePalette(COLORREF palette[], int fontX, int fontY, wchar_t *fontName) { int i; HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); /* Retrieving pointers for Windows Vista/7 Functions */ PGetCurrentConsoleFontEx pGetCurrentConsoleFontEx = (PGetCurrentConsoleFontEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "GetCurrentConsoleFontEx"); PSetCurrentConsoleFontEx pSetCurrentConsoleFontEx = (PSetCurrentConsoleFontEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetCurrentConsoleFontEx"); PGetConsoleScreenBufferInfoEx pGetConsoleScreenBufferInfoEx = (PGetConsoleScreenBufferInfoEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "GetConsoleScreenBufferInfoEx"); PSetConsoleScreenBufferInfoEx pSetConsoleScreenBufferInfoEx = (PSetConsoleScreenBufferInfoEx) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetConsoleScreenBufferInfoEx"); /* Check for pointers: if they exist, we have Vista/7 and we can use them */ if (pGetCurrentConsoleFontEx && pSetCurrentConsoleFontEx && pGetConsoleScreenBufferInfoEx && pSetConsoleScreenBufferInfoEx) { CONSOLE_SCREEN_BUFFER_INFOEX cbi; CONSOLE_FONT_INFOEX cfi = {sizeof(CONSOLE_FONT_INFOEX)}; /* Tell the font info how big it is, to avoid memory corruption */ cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX); pGetCurrentConsoleFontEx(hOutput, TRUE, &cfi); /* Set the font type to name indicated by wide string literal */ /* Set to 0 to keep current settings */ lstrcpyW(cfi.FaceName, fontName); cfi.dwFontSize.X = fontX; cfi.dwFontSize.Y = fontY; cfi.FontFamily = 0; /* Set to 0x30 for Terminal font */ cfi.FontWeight = 0; /* Set the console font info */ pSetCurrentConsoleFontEx(hOutput, TRUE, &cfi); /* Get the size of the structure before filling the struct */ cbi.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX); pGetConsoleScreenBufferInfoEx(hOutput, &cbi); /* Loop through the given palette, copying the data into the color table */ for (i = 0 ; i < 16 ; ++i) { cbi.ColorTable[i] = palette[i]; } /* Set the console screen buffer info */ pSetConsoleScreenBufferInfoEx(hOutput, &cbi); } else /* We don't have access to the vista functions */ { int i; CONSOLE_INFO ci = { sizeof(ci) }; HWND hwndConsole = GetConsoleWindow(); GetConsoleSizeInfo(&ci, hOutput); /* Set to 0 to keep current settings */ ci.FontSize.X = fontX; ci.FontSize.Y = fontY; ci.FontFamily = 0; /* Set to 0x30 for Terminal font */ ci.FontWeight = 0; lstrcpyW(ci.FaceName, fontName); ci.CursorSize = 100; ci.FullScreen = FALSE; ci.QuickEdit = FALSE; ci.AutoPosition = 0x10000; ci.InsertMode = TRUE; ci.ScreenColors = MAKEWORD(0x7, 0x0); ci.PopupColors = MAKEWORD(0x5, 0xf); ci.HistoryNoDup = TRUE; ci.HistoryBufferSize = 50; ci.NumberOfHistoryBuffers = 4; for(i = 0; i < 16; i++) { ci.ColorTable[i] = palette[i]; } ci.CodePage = 0; ci.Hwnd = hwndConsole; lstrcpyW(ci.ConsoleTitle, L""); SetConsoleInfo(hwndConsole, &ci); } }