Thread: Colour Font

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    Colour Font

    Code:
    #include <stdio.h> 
    #include <windows.h> 
    
    int main ( void )
    {
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
      
      printf ( "This is a test\n" );
    
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
      return 0;
    }
    mind explaning to me how those codes work ?
    1 more question ...
    when i add system("color 1F") at the beginning of the codes above, the background of "This is a test" is still black colour, how do i change it to become blue background ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > mind explaning to me how those codes work ?
    Read the manual page for each of those functions would be my advice

    > when i add system("color 1F") at the beginning of the codes above
    system() creates another (short-lived) shell, changes the colour in that sub-shell and exits. The current shell remains as it was

    > how do i change it to become blue background ?
    On your way through reading the manual on the functions above, watch out for any which mention "background" in the list of related functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    mind explaning to me how those codes work ?
    Well, in general, <windows.h> contains thousands of functions, constants, structures, and a few typedefs. For example, I believe FOREGROUND_INTENSITY is an enumerated constant. (You could display it with cout and you would see its numerical value.)

    If you want specifics... if you want to know what SetConsoleTextAttribute() does and how to use it, you can look it up in your compiler's documentation / help.

    If you want to learn about all of the WinAPI functions you have to get a copy of Programming Windows buy Charles Petzold. But of course, he doesn't talk much about the console functions.

    I believe if you want to change the entire console background, you have to "re-draw" the entire screen. Off-hand I don't know how to do this... It's probably possible to determine the color before the console window is created... but I don't know how to do that either.

    And remember... None if this is standard-portable C++. It only works on Windows computers and only compiles with a Windows compiler.
    Last edited by DougDbug; 10-24-2003 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  2. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Font Colour
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-13-2001, 12:55 PM