Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Console Output Formatting

Console Output Formatting

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brett Peirce
    wrote on last edited by
    #1

    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.

    E 1 Reply Last reply
    0
    • B Brett Peirce

      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.

      E Offline
      E Offline
      EXTEIDE
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups