Console Output Formatting
-
I am trying to output to the console and trying to center words on the screen. I realize that I cold assume 80 characters can be displayed across - its pretty much the absolute standard these days - but I was wondering if anyone knows a function to get the character width of the console output so that I can use that to be a little more universal in my centering function. Anyone know?
sincerely, Brett Peirce - PolerBear To err is human; To forgive: divine. -
I am trying to output to the console and trying to center words on the screen. I realize that I cold assume 80 characters can be displayed across - its pretty much the absolute standard these days - but I was wondering if anyone knows a function to get the character width of the console output so that I can use that to be a little more universal in my centering function. Anyone know?
sincerely, Brett Peirce - PolerBear To err is human; To forgive: divine.I found the code that you want in here. http://www.koders.com/c/fidD1457B7594BFF764B76CFFDBCD7E2EDE2E4188C3.aspx?s=telnet And I tested it like below, and I could get the result; // consoletest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include void SetConsoleSize(HANDLE hConsole, SHORT xSize, SHORT ySize) { CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */ BOOL bSuccess; SMALL_RECT srWindowRect; /* hold the new console size */ COORD coordScreen; bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi); /* get the largest size we can size the console window to */ coordScreen = GetLargestConsoleWindowSize(hConsole); /* define the new console window size and scroll position */ srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1); srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1); srWindowRect.Left = srWindowRect.Top = (SHORT) 0; /* define the new console buffer size */ coordScreen.X = xSize; coordScreen.Y = ySize; /* if the current buffer is larger than what we want, resize the */ /* console window first, then the buffer */ if ((DWORD) csbi.dwSize.X * csbi.dwSize.Y > (DWORD) xSize * ySize) { bSuccess = SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect); bSuccess = SetConsoleScreenBufferSize(hConsole, coordScreen); } /* if the current buffer is smaller than what we want, resize the */ /* buffer first, then the console window */ if ((DWORD) csbi.dwSize.X * csbi.dwSize.Y < (DWORD) xSize * ySize) { bSuccess = SetConsoleScreenBufferSize(hConsole, coordScreen); bSuccess = SetConsoleWindowInfo(hConsole, TRUE, &srWindowRect); } /* if the current buffer *is* the size we want, don't do anything! */ return; } int main(int argc, char* argv[]) { HANDLE hStdout; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleSize(hStdout, 100, 100); printf("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n"); getch(); return 0; } Enjoy:rose: Anderson Sheen http://www.exteide.com -- modified at 22:04 Friday 10th February, 2006