943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1996
  • C++ RSS
Mar 30th, 2009
0

Console application colors?

Expand Post »
Hi,

Is it possible (using VC++6) to change background and forground text colors when builing a simple console application?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Willco is offline Offline
4 posts
since Mar 2009
Mar 30th, 2009
0

Re: Console application colors?

Certainly.

SetConsoleTextAttribute()

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Mar 30th, 2009
0

Re: Console application colors?

Cheers Buddy, for anyone else out there...

C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <iostream.h>
  3. #include <stdio.h>
  4.  
  5. int main() //Visual C++ Console Application. to Demo Color Text.
  6. {
  7. HANDLE hOut;
  8. int a;
  9.  
  10. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  11.  
  12. SetConsoleTextAttribute(hOut,30); //HI yellow on BLUE.
  13. system("cls");
  14.  
  15. printf("Hello World \n\r");
  16. printf("Press any key then enter to exit");
  17.  
  18. cin >> a;
  19.  
  20. return 0;
  21. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Willco is offline Offline
4 posts
since Mar 2009
Mar 30th, 2009
0

Re: Console application colors?

Click to Expand / Collapse  Quote originally posted by Willco ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Mar 31st, 2009
0

Re: Console application colors?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Willco is offline Offline
4 posts
since Mar 2009
Mar 31st, 2009
0

Re: Console application colors?

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Willco is offline Offline
4 posts
since Mar 2009
Mar 31st, 2009
0

Re: Console application colors?

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

C++ Syntax (Toggle Plain Text)
  1. //Do this somewhere inside your code. "hOutput" must be global.
  2. HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  3.  
  4.  
  5. void zprintf(const char *format, ...)
  6. {
  7. char syntax[4096*2] = {0};
  8. va_list args;
  9. va_start(args,format);
  10. vsprintf(syntax,format,args);
  11. va_end(args);
  12.  
  13. char *s = syntax;
  14. char code[3] = {0};
  15. char *end = NULL;
  16. while(*s){
  17. if(*s == '<' && s[1] == 'c'){
  18. int len = strcspn(s+2,">");
  19. memcpy(code,++(++s),len);
  20. unsigned long color = strtoul(code,&end,0);
  21. SetConsoleTextAttribute(hOutput,color);
  22. s+=len+1;
  23. }else printf("%c",*s++);
  24. }
  25. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jazu100 is offline Offline
10 posts
since Aug 2008
Mar 31st, 2009
0

Re: Console application colors?

Click to Expand / Collapse  Quote originally posted by Willco ...
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. int main()
  3. {
  4. std::cout << "Hello World\n";
  5. }

Click to Expand / Collapse  Quote originally posted by Willco ...
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.
Last edited by Ancient Dragon; Mar 31st, 2009 at 9:31 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Mar 31st, 2009
0

Re: Console application colors?

Click to Expand / Collapse  Quote originally posted by Willco ...
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.
Last edited by NicAx64; Mar 31st, 2009 at 11:00 am.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 31st, 2009
0

Re: Console application colors?

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

C++ Syntax (Toggle Plain Text)
  1. 1: // q3stl.cpp : Defines the entry point for the console application.
  2. 2: //
  3. 3:
  4. 4: #include "stdafx.h"
  5. 5:
  6. 6: int main(int argc, char* argv[])
  7. 7: {
  8. 00401010 push ebp
  9. 00401011 mov ebp,esp
  10. 00401013 sub esp,40h
  11. 00401016 push ebx
  12. 00401017 push esi
  13. 00401018 push edi
  14. 00401019 lea edi,[ebp-40h]
  15. 0040101C mov ecx,10h
  16. 00401021 mov eax,0CCCCCCCCh
  17. 00401026 rep stos dword ptr [edi]
  18. 8: printf("Hello World!\n");
  19. 00401028 push offset string "Hello World!\n" (0042001c)
  20. 0040102D call printf (00401060)
  21. 00401032 add esp,4
  22. 9: return 0;
  23. 00401035 xor eax,eax
  24. 10: }
  25. 00401037 pop edi
  26. 00401038 pop esi
  27. 00401039 pop ebx
  28. 0040103A add esp,40h
  29. 0040103D cmp ebp,esp
  30. 0040103F call __chkesp (004010e0)
  31. 00401044 mov esp,ebp
  32. 00401046 pop ebp
  33. 00401047 ret
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compiler errors lnk2019, lnk1120
Next Thread in C++ Forum Timeline: Help with header file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC