Moving the Console Cursor & Printing In Color

This is a discussion on Moving the Console Cursor & Printing In Color within the C Programming forums, part of the General Programming Boards category; Just a simple sample program of moving the console cursor, in Windows, and printing with colored text or background. Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    Moving the Console Cursor & Printing In Color

    Just a simple sample program of moving the console cursor, in Windows, and printing with colored text or background.

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <wincon.h>
    
    
    void Gotoxy(int, int);
    
    int main(void)  {
       int x, y;
       
       char *starlight = "Oh Be a Fine Girl Kiss Me";
       char *ilike = "I like blue stars";
       for(x = 0, y = 1; x < 41;)  {
          Gotoxy(x,y);
    	  printf("%s", ilike);  
    	  x += strlen(ilike)+1;
    	  
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_BLUE|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);	
       printf("\n%s\n ", ilike); 
       for(y = 4, x = 10; y < 18; y+=5)  {
          Gotoxy(x, y);
          printf("%s", starlight); 
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|BACKGROUND_RED);
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|BACKGROUND_BLUE);	
       printf("\n\n\n%s", starlight);
       printf("\n\n");   
       return 0;
    }
    
    
    void Gotoxy(int x, int y) {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    //BLUE |GREEN|RED = White or Grey, depending on Intensity
    //All combo's are the same as mixing primary colors of light (not paint).
    Last edited by Adak; 03-09-2009 at 11:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. custom console color
    By Syneris in forum Windows Programming
    Replies: 2
    Last Post: 04-05-2002, 04:23 PM
  2. Color Glitches in Console Prog.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2002, 10:52 PM
  3. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM
  4. Printing with Color Letters
    By BigSter in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 10:42 PM
  5. About Color in MFC
    By Alextrons in forum Windows Programming
    Replies: 0
    Last Post: 11-05-2001, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21