?
Solved

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

Posted on 2003-02-19
17
Medium Priority
?
671 Views
Last Modified: 2013-12-14
#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
0
Comment
Question by:joely2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 1

Expert Comment

by:makkbru
ID: 7980673
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
 

Author Comment

by:joely2k
ID: 7980833
Do i need to include any special header file??
i mean #include<????????.h>
0
 

Author Comment

by:joely2k
ID: 7980890
Why when i execute your code..it told me that the hwnd handler is not initialize or something..
please help
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:xyzzer
ID: 7987064
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
 
LVL 1

Expert Comment

by:makkbru
ID: 7992087
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
 
LVL 1

Expert Comment

by:makkbru
ID: 7992096
ohhh.... and you need to include windows.h....
0
 
LVL 1

Expert Comment

by:makkbru
ID: 7992140
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
 

Author Comment

by:joely2k
ID: 7995029
#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
 
LVL 2

Expert Comment

by:xyzzer
ID: 7998509
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
 

Author Comment

by:joely2k
ID: 7999487
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
 
LVL 2

Expert Comment

by:xyzzer
ID: 8003467
#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
 

Author Comment

by:joely2k
ID: 8005277
I can't use that in microsoft visual C++...

0
 
LVL 2

Expert Comment

by:xyzzer
ID: 8006741
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
 

Author Comment

by:joely2k
ID: 8012475
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
 
LVL 2

Accepted Solution

by:
xyzzer earned 80 total points
ID: 8014645
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
 

Expert Comment

by:SpideyMod
ID: 8103481
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
 

Expert Comment

by:SpideyMod
ID: 8132583
Force Accepted xyzzer's answer

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Introduction to Processes

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question