How to use colour in Microsoft Visual C++??

#include <iostream.h>

main ()
{
   cout<<"Harlow!!"<<endl;
}

Simple Question...how can I make the string "Harlow!!"
into a color string when we output....any color will do..
just show me how please

Thanks
joely2kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

makkbruCommented:
you can use the windows GDI (graphics device interface).
this allows you to draw things like circles,rectangles and also text.

PAINTSTRUCT     ps;
HDC          hdc;
       
int xposition=0;
int yposition=0;

hdc=BeginPaint(hwnd,&ps);

SetTextColor(hdc,RGB(0,0,255));
SetBkColor(hdc,RGB(0,0,0));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,xposition,yposition,"Harlow",strlen("Harlow"));

EndPaint(hwnd,&ps);    

the problems with this is that you have to use windowsprogramming, and you have to spesify the position of the text.

hdc is a handle to the GDI device (or something like that; i cant remember).

hwnd is a handle to a windows window.
0
joely2kAuthor Commented:
Do i need to include any special header file??
i mean #include<????????.h>
0
joely2kAuthor Commented:
Why when i execute your code..it told me that the hwnd handler is not initialize or something..
please help
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

xyzzerCommented:
You need to use WinAPI if you want to do it in Windows (probably X-Window under Linux). It involves a few long and boring steps of instantiating the application, creating a window class and message loop, where you put the above code under the WM_PAINT message. You can look for a generic Windows application if you want to start.
It all depends on how much you know, how much you want to know and what tools you have at disposal.
If you want to start programming and have a simple tool as Borland C++ 3.1, you can add the following:

#include <conio.h>

  textcolor(a_color_number_to_set);

and make a simple DOS application

Doing it in Windows might require more than just iostream in a console.
You can use some RAD tools like Borland C++ Builder or Microsoft Visual Studio to speed up the process of building an application, but it's better to start from the bottom.

--Filip
0
makkbruCommented:
xyzzers way is probably what you want....
in windows you have to add a lot of additional code just to open a visible window.

//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        break;
        case WM_KEYUP:
            switch (wParam)
            {
                case VK_ESCAPE:
                    DestroyWindow(hWnd);
                    return 0;
                break;
            }
        break;

    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}//end winproc

//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
    //Register the window class
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "DX Project 1", NULL};

    RegisterClassEx(&wc);

    //Create the application's window
    HWND hWnd = CreateWindow("DX Project 1", mywindowname.com",WS_OVERLAPPEDWINDOW, 50, 50, 500, 500, GetDesktopWindow(), NULL, wc.hInstance, NULL);
     
        //Show our window
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);
///////////////////

//your code goes here


/////////////////////

return 0;
}//end winmain

this is the code for opening a window in windows....(please dont ask me to explane all that....) i copied this sample from andypike.com directx tutorial 1. you can find an explanation there...

but you'll probably want to use xyzzers way....
0
makkbruCommented:
ohhh.... and you need to include windows.h....
0
makkbruCommented:
xyzzer:
I cant get your code to work?
error C2065: 'textcolor' : undeclared identifier

???
are you shure conio.h is the right headerfile...

what color format dows the function take?
0
joely2kAuthor Commented:
#include <iostream.h>
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
#include <conio.h>


int main ()
{
     HANDLE change;
     WORD color=0;
     WORD background=0;
     WORD normal;

     
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     

     if((change = CreateFile("CONOUT$", GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L) )== (HANDLE) -1 )
     {
          cout<<endl<<"Error: Unable to open console"<<endl;
          return( -1 );
     }
     GetConsoleScreenBufferInfo( change, &csbi );

     normal=csbi.wAttributes;

     
     color=14;
     background=0;

     SetConsoleTextAttribute( change, (WORD)((background<<7)|color));

     cout<<"Harlow my Name is Joel";
     cout<<"\nThis line also yellow I think??"<<endl;

     SetConsoleTextAttribute(change, normal);

     cout<<"\nThis color is back to normal I think..."<<endl;


     getch();

     return(0);

}


this is a code create from a friend of mine and it works..
I modified and simplified alot...
what does it different from yours makkbru??
can you teach me..??
0
xyzzerCommented:
The information copied from BCBuilder's 5 help file:

Header File

conio.h

Category

Console I/O Routines

Syntax

#include <conio.h>
void textcolor(int newcolor);

Description

Selects new character color in text mode.

Note:     Do not use this function for Win32s or Win32 GUI applications.

textcolor selects the foreground character color. This function works for the console output functions. newcolor selects the new foreground color. You can set newcolor to an integer as given in the table below, or to one of the symbolic constants defined in conio.h. If you use symbolic constants, you must include conio.h.

Once you have called textcolor, all subsequent functions using direct video output (such as cprintf) will use newcolor. textcolor does not affect any characters currently onscreen.

The following table lists the allowable colors (as symbolic constants) and their numeric values:

Symbolic constant     Numeric value

BLACK     0
BLUE     1
GREEN     2
CYAN     3
RED     4
MAGENTA     5
BROWN     6
LIGHTGRAY     7
DARKGRAY     8
LIGHTBLUE     9
LIGHTGREEN     10
LIGHTCYAN     11
LIGHTRED     12
LIGHTMAGENTA     13
YELLOW     14
WHITE     15
BLINK     128
0
joely2kAuthor Commented:
I understand your point xyzzer..
but can you please write me the simple code for me to execute some colors text please?
so I can just copy and try
in microsoft visual C++ instead of using and include so many routines and commands....
your help will be superbly appreciated
0
xyzzerCommented:
#include <stdio.h>
#include <conio.h>
void main(void) {
  textcolor(GREEN);
  printf("That's in green\n");
  textcolor(LIGHTGRAY);
  printf("That's in lightgray\n");
  textcolor(LIGHTGREEN);
  printf("That's in lightgreen\n");
}

That requires that the code is run in a console (DOS?) mode.
0
joely2kAuthor Commented:
I can't use that in microsoft visual C++...

0
xyzzerCommented:
Then You should check the WinAPI GDI solution above, study a little bit (I can recommend a book by Charles Petzold - Programming Windows), or try different project settings, or look for a book/tutorial on VS, or try a simplier compiler - maybe a free Open Watcom (http://www.openwatcom.org/download/download_licenses.html).

Filip Skakun
0
joely2kAuthor Commented:
i seee.... anyway thanks for your help alot man..
can you introduce me some website that can learn C++ and master it superbly?
I really wanna train hard....

besides that any recomendation complilers for cobol and pascal??
Since you help me and explain to me the most..
the points should be credit to you....xyzzer
thanks
0
xyzzerCommented:
If I were You I would start simple - Look for Borland C++ 3.1 and Borland/Turbo Pascal 7. Those are the latest of DOS compilers and should be free by now. Only - I don't know where to look for them apart from my CDs...

--Filip
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SpideyModCommented:
joely2k,
Please return to this question.  An expert has assisted you since your last response.  If you have a good answer, please accept it.  If you need more information, please ask.  Remember you should always be the last person to post in your own questions.  I will return to finalize this question in 72 hours.  As xyzzer was the last expert to have assisted you after you came back with responses, I will most likely award the points to that expert unless you take charge of this question again.  If anybody(including joely2k) has any other proposed disposition for this question, feel free to leave it here before the 72 hour point is reached.  Thanks all.

SpideyMod
Community Support Moderator @Experts Exchange
0
SpideyModCommented:
Force Accepted xyzzer's answer

SpideyMod
Community Support Moderator @Experts Exchange
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.