Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 149,628 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,012 people online right now. Registration is fast and FREE... Join Now!





Windows Console Output

Demonstrates using the Win32 console API to manipulate console output. *Updated to compile with Dev-C++ 4.9.9.2 -- resolved overloaded function names.

Submitted By: NickDMax
Actions:
Rating:
Views: 7,986

Language: C

Last Modified: August 5, 2008
Instructions: This example requires Windows 98 or better.

Snippet


  1. /*****************************************************************
  2. * WinConsole.c: Demonstrates using the Win32 console API to
  3. * manipulate console output.
  4. * Program by: NickDMax
  5. *****************************************************************/
  6.  
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10.  
  11.  
  12. void ConPrint(char *CharBuffer, int len);
  13. void ConPrintAt(int x, int y, char *CharBuffer, int len);
  14. void gotoXY(int x, int y);
  15. void ClearConsole(void);
  16. void ClearConsoleToColors(int ForgC, int BackC);
  17. void SetColorAndBackground(int ForgC, int BackC);
  18. void SetColor(int ForgC);
  19. void HideTheCursor(void);
  20. void ShowTheCursor(void);
  21.  
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25.      HideTheCursor();
  26.      ClearConsoleToColors(15, 1);
  27.      ClearConsole();
  28.      gotoXY(1,1);
  29.      SetColor(14);
  30.      printf("This is a test...\n");
  31.      Sleep(1000);
  32.      ShowTheCursor();
  33.      SetColorAndBackground(15,12);
  34.      ConPrint("This is also a test...\n", 23);
  35.      SetColorAndBackground(1, 7);
  36.      ConPrintAt(22, 15, "This is also a test...\n", 23);
  37.      gotoXY(0, 24);
  38.      SetColorAndBackground(7,1);
  39.      return 0;
  40. }
  41.  
  42.  
  43. //This will clear the console while setting the forground and
  44. //  background colors.
  45. void ClearConsoleToColors(int ForgC, int BackC)
  46. {
  47.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  48.      //Get the handle to the current output buffer...
  49.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  50.      //This is used to reset the carat/cursor to the top left.
  51.      COORD coord = {0, 0};
  52.      //A return value... indicating how many chars were written
  53.      //   not used but we need to capture this since it will be
  54.      //   written anyway (passing NULL causes an access violation).
  55.      DWORD count;
  56.  
  57.      //This is a structure containing all of the console info
  58.      // it is used here to find the size of the console.
  59.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  60.      //Here we will set the current color
  61.      SetConsoleTextAttribute(hStdOut, wColor);
  62.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  63.      {
  64.           //This fills the buffer with a given character (in this case 32=space).
  65.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  66.          
  67.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  68.           //This will set our cursor position for the next print statement.
  69.           SetConsoleCursorPosition(hStdOut, coord);
  70.      }
  71.      return;
  72. }
  73.  
  74. //This will clear the console.
  75. void ClearConsole()
  76. {
  77.      //Get the handle to the current output buffer...
  78.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  79.      //This is used to reset the carat/cursor to the top left.
  80.      COORD coord = {0, 0};
  81.      //A return value... indicating how many chars were written
  82.      //   not used but we need to capture this since it will be
  83.      //   written anyway (passing NULL causes an access violation).
  84.      DWORD count;
  85.      //This is a structure containing all of the console info
  86.      // it is used here to find the size of the console.
  87.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  88.      //Here we will set the current color
  89.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  90.      {
  91.           //This fills the buffer with a given character (in this case 32=space).
  92.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  93.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  94.           //This will set our cursor position for the next print statement.
  95.           SetConsoleCursorPosition(hStdOut, coord);
  96.      }
  97.      return;
  98. }
  99.  
  100. //This will set the position of the cursor
  101. void gotoXY(int x, int y)
  102. {
  103.      //Initialize the coordinates
  104.      COORD coord = {x, y};
  105.      //Set the position
  106.      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  107.      return;
  108. }
  109.  
  110. //This will set the forground color for printing in a console window.
  111. void SetColor(int ForgC)
  112. {
  113.      WORD wColor;
  114.      //We will need this handle to get the current background attribute
  115.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  116.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  117.      
  118.      //We use csbi for the wAttributes word.
  119.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  120.      {
  121.         //Mask out all but the background attribute, and add in the forgournd color
  122.           wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  123.           SetConsoleTextAttribute(hStdOut, wColor);     
  124.      }
  125.      return;
  126. }
  127.  
  128. //This will set the forground and background color for printing in a console window.
  129. void SetColorAndBackground(int ForgC, int BackC)
  130. {
  131.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
  132.      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);     
  133.      return;
  134. }
  135.  
  136. //Direct console output
  137. void ConPrint(char *CharBuffer, int len)
  138. {
  139.     DWORD count;
  140.      WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
  141.      return;
  142. }
  143.  
  144. //Direct Console output at a particular coordinate.
  145. void ConPrintAt(int x, int y, char *CharBuffer, int len)
  146. {
  147.     DWORD count;
  148.      COORD coord = {x, y};
  149.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  150.     SetConsoleCursorPosition(hStdOut, coord);
  151.      WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
  152.      return;
  153. }
  154.  
  155. //Hides the console cursor
  156. void HideTheCursor()
  157. {
  158.      CONSOLE_CURSOR_INFO cciCursor;
  159.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  160.      
  161.      
  162.      if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  163.      {
  164.           cciCursor.bVisible=FALSE;
  165.      }
  166.      return;
  167. }
  168.  
  169. //Shows the console cursor
  170. void ShowTheCursor()
  171. {
  172.      CONSOLE_CURSOR_INFO cciCursor;
  173.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  174.      
  175.      
  176.      if(GetConsoleCursorInfo(hStdOut, &cciCursor))
  177.      {
  178.           cciCursor.bVisible=TRUE;
  179.      }
  180.      return;
  181. }
  182.  
  183.  

Copy & Paste


Comments


born2c0de 2007-11-19 23:10:49

Brilliant use of Windows Console API!! I'm not sure if you're trying to reset the colors at the end of main(), but if you are: SetColor(7,1); should be SetColor(7,0);

NickDMax 2007-11-22 09:35:25

I always liked the white on blue. That is the default on my system (has been since the days of DOS 3.1).

NickDMax 2007-12-04 11:21:34

I added another snippet that extends this a bit, it adds the ability to printfAt(). That can be done in this program using gotoXY() and then printf(), but this does it all at once: http://www.dreamincode.net/code/snippet1504.htm


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month