Thread: Color text from windows.h?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    13

    Color text from windows.h?

    Well im making a text based game like in a console...so I was wondering what headers Id need..does the windows.h header have color text options? Ithought it does but I cant find anything on it..and when I do find somthing it never works.

    Also is ncurses *nix only?

    And what other headers do you think id need for making this text based game..right now its more of a paper and mind thing im just going to start making the format of the game right now..then code the stats system.. which will take me awhile..

    -- Paul

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are cross-platform curses implementations, at least partial ones. I personally had very little luck finding them, though.

    For a very basic text-based game, <iostream> is the only header you need for I/O (you might want to have <string> and various others, though).

    <windows.h> does indeed contain a few functions for very basic text colouring.

    You're starting at the wrong end, anyway. Never worry what headers you need. You should worry about what code you need. Then you check what parts of the code already exist somewhere. Then you look up the headers that contain the declarations.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In case you haven't looked at it yet, there's an article about colouring text in the FAQ here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    The options there look a little limited, but it should probably be adequate for a simple game. I should add though, I've never used coloured text before myself, so I know very little about it.
    Last edited by Hunter2; 12-02-2004 at 03:34 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    Ive tried that in the FAQ's and its not working..So I must be doing somthing wrong..

    The game will be somewhat complex after awhile..and I just need basic colored font.. thats it...not background colors or anything just plain like blue,green,red,yellow,ect..

    LMAO back in the QBASIC days color text was so easy COLOR 1 or COLOR 5 haha... man its so confusing in C++

    -- Paul

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Ive tried that in the FAQ's and its not working..So I must be doing somthing wrong..
    Can you be more specific about what the problem is?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    
    int main ( void )
    {
      textcolor ( MAGENTA );
      cprintf ( "This is a test\n" );
    
      return 0;
    }
    Thats what it says to use..but this returns me errors...

    Does anyone have any other links to basic color font..

    -- Paul

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Saintdog, that's what it says to use for DOS or compilers that have the textcolor function (I think Borland's does). VC++, which you're probably using, is not among them. Scroll a bit down and try the Win32 API approach.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You can do colored text quite easily using the windows console functions

    Code:
    HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE);//gets stdout handle used for output
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);//gets stdin handle used for input
    DWORD num; //used to keep track of number of char written to screen
     
    SetConsoleTextAttribute(FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN|BACKGROUND_BLUE);// sets color 
    WriteConsole(hStdout,"TEXT",strlen("TEXT"),&num,NULL);//writes text to screen
    http://msdn.microsoft.com/library/de...tattribute.asp check MSDN Library online for all the console functions
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    No im using Bloodshed Dev-C++..but is there a better free one in your opinion..or one you have to pay for that is good?

    And ill try the Win32 API stuff here after dinner time.

    Thanks for your help so far and hope I didnt make anyone mad for asking a dumb question.

    -- Paul

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Aha, from this thread:
    http://cboard.cprogramming.com/showt...ght=text+color
    Quote Originally Posted by red_baron
    ok much simpler it is basically this:

    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), colour#);

    so just put in a number where colour# is anything between 0 and 256 then everything you print after that code will be in that colour.
    This looks more promising Plenty of colours to choose from. Here's a quick program that'll help you find the right 'number' for the colour you want:
    Code:
    #include <iostream>
    #include <windows.h>
    
    int main()
    {
    	for(int i = 0; i < 256; ++i)
    	{
    		SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), i);
    		std::cout << i << ' ';
    	}
    	std::cout << std::endl;
    	std::cin.get();
    
    	return 0;
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    My 6 part console programming tutorial here covers colouring and a whole load more.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading windows.h
    By yaya in forum Windows Programming
    Replies: 7
    Last Post: 05-22-2009, 02:33 PM
  2. windows.h no such file or directory
    By FingerPrint in forum Tech Board
    Replies: 11
    Last Post: 08-26-2006, 09:51 PM
  3. Windows.h help
    By Mustang5670 in forum C Programming
    Replies: 3
    Last Post: 01-02-2004, 11:59 AM
  4. UNICODE and windows.h help
    By nextus in forum Windows Programming
    Replies: 3
    Last Post: 03-02-2003, 03:13 PM
  5. windows.h without the mouse support
    By ()Q() in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2002, 07:57 PM