Hi,

Is it possible (using VC++6) to change background and forground text colors when builing a simple console application?

Recommended Answers

All 9 Replies

Cheers Buddy, for anyone else out there...

#include <windows.h>
#include <iostream.h>
#include <stdio.h>

int main()  //Visual C++  Console Application. to Demo Color Text.
{
    HANDLE hOut;
	int a;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

	SetConsoleTextAttribute(hOut,30); //HI yellow on BLUE.
	system("cls"); 

	printf("Hello World \n\r");
	printf("Press any key then enter to exit");

	cin >> a;
   
    return 0;
}

Is it possible (using VC++6) ...?

Get a newer compiler. You can download free Microsoft VC++ 2008 Express, but it doesn't do MFC. But it is a lot more compliant with c++ standards than 6.0 version.

Get a newer compiler. You can download free Microsoft VC++ 2008 Express, but it doesn't do MFC. But it is a lot more compliant with c++ standards than 6.0 version.

hmm, those will be the same standards that took a perfectly reasonable language and loaded layer after layer of garbage on the top such that "Hello World" which used to be 5 lines long and exe at 20K is now well over 150 lines and makes an EXE of almost 1MB. My applications require speed, up till now I have used Visual Basic as an interface and built (C DLL's to control production process hardware (under Win98) . I looked at 2008 and decided it is yet another piece of software that deserves the "Emporers clothes" awards. Anyway that's a personal opinion (maybe I'm just stuck in my ways..) Thanks for the replies.

Ah, now I remember why I Don't use 2008, with VC6 I can build a release version and transport a single EXE file to any of the family PC's and it will run. With 2008 it will only run on the machine on which it was made I need to write code that can be used on other machines..

My cool "zprintf".
You can change color attributes inside string, like
<c10>Hi <c12>There<c14>!!!!
And you get rid of those SetConsoleTextAttribute -lines!

//Do this somewhere inside your code. "hOutput" must be global.
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);


void zprintf(const char *format, ...)
{
     char syntax[4096*2] = {0};
     va_list args;
     va_start(args,format);
     vsprintf(syntax,format,args);
     va_end(args);
     
     char *s = syntax;
     char code[3] = {0};
     char *end = NULL;
     while(*s){
          if(*s == '<' && s[1] == 'c'){
               int len = strcspn(s+2,">");
               memcpy(code,++(++s),len);
               unsigned long color = strtoul(code,&end,0);
               SetConsoleTextAttribute(hOutput,color);
               s+=len+1;
          }else printf("%c",*s++);
     }
}

hmm, those will be the same standards that took a perfectly reasonable language and loaded layer after layer of garbage on the top such that "Hello World" which used to be 5 lines long and exe at 20K is now well over 150 lines and makes an EXE of almost 1MB.

Its quite obvious you don't know what you are talking about. Hello World is still just a few lines long, and release build compiles down to 8,704 bytes, less than half the size of your 20K program.

#include <iostream>
int main()
{
    std::cout << "Hello World\n";
}

My applications require speed, up till now I have used Visual Basic as an interface and built (C DLL's to control production process hardware (under Win98) .

That's an oxymoron statement!! You can't get speed out of VB or Win98. Come into the 21st Century by upgrading hardware and os to XP.

hmm, those will be the same standards that took a perfectly reasonable language and loaded layer after layer of garbage on the top such that "Hello World" which used to be 5 lines long and exe at 20K is now well over 150 lines and makes an EXE of almost 1MB. My applications require speed, up till now I have used Visual Basic as an interface and built (C DLL's to control production process hardware (under Win98) . I looked at 2008 and decided it is yet another piece of software that deserves the "Emporers clothes" awards. Anyway that's a personal opinion (maybe I'm just stuck in my ways..) Thanks for the replies.

Answer for the Executable Size:

Things will never happen like that don't worry. In a executable file ( windows PE exectuable ) there are file format helders. Most of the space are gone for them plus another half of space is gone to the debug information , VC++ 6.0 default build the debug. So that's why you see your'r binaries big.

Another reason for that is just your usage of static libraries. Think that you are using STL , STL is a static library in many plactforms.
Surelly in the windows.
But the code that you writes never increase the size like that. you just can open the disassembler and see how many assembly instructions your code actually get.
Just , compile and build your project. and add a break point to the int main () line.and the , press
build->start debug -> go
and the debugger will stop before executing the line that contains the main. and then
view menu -> debug windows -> disassembly
and you will see how much assembly instructions that each line takes.


Answer for the Speed:
well that's true you cannot expect the real time behaviour under a general purpose operating system( like windows XP , your 98 even, and linux ). So therefore you need to use a realtime os for that. ( if you really wish ). I think go to the old MS-DOS world back is really a bad idea. But if you really interested you can use a morden real time operating system for this. I recommand you to use ecos operating system for this. Ecos is open source and you can do many fun projects with cheap hardware even.

for me this is only the code space that taken to that 5 lines long "Hello World" application.

1:    // q3stl.cpp : Defines the entry point for the console application.
2:    //
3:
4:    #include "stdafx.h"
5:
6:    int main(int argc, char* argv[])
7:    {
00401010   push        ebp
00401011   mov         ebp,esp
00401013   sub         esp,40h
00401016   push        ebx
00401017   push        esi
00401018   push        edi
00401019   lea         edi,[ebp-40h]
0040101C   mov         ecx,10h
00401021   mov         eax,0CCCCCCCCh
00401026   rep stos    dword ptr [edi]
8:        printf("Hello World!\n");
00401028   push        offset string "Hello World!\n" (0042001c)
0040102D   call        printf (00401060)
00401032   add         esp,4
9:        return 0;
00401035   xor         eax,eax
10:   }
00401037   pop         edi
00401038   pop         esi
00401039   pop         ebx
0040103A   add         esp,40h
0040103D   cmp         ebp,esp
0040103F   call        __chkesp (004010e0)
00401044   mov         esp,ebp
00401046   pop         ebp
00401047   ret
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.