Console application colors?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 4
Reputation: Willco is an unknown quantity at this point 
Solved Threads: 0
Willco Willco is offline Offline
Newbie Poster

Console application colors?

 
0
  #1
Mar 30th, 2009
Hi,

Is it possible (using VC++6) to change background and forground text colors when builing a simple console application?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Console application colors?

 
0
  #2
Mar 30th, 2009
Certainly.

SetConsoleTextAttribute()

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: Willco is an unknown quantity at this point 
Solved Threads: 0
Willco Willco is offline Offline
Newbie Poster

Re: Console application colors?

 
0
  #3
Mar 30th, 2009
Cheers Buddy, for anyone else out there...

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,628
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Console application colors?

 
0
  #4
Mar 30th, 2009
Originally Posted by Willco View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: Willco is an unknown quantity at this point 
Solved Threads: 0
Willco Willco is offline Offline
Newbie Poster

Re: Console application colors?

 
0
  #5
Mar 31st, 2009
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: Willco is an unknown quantity at this point 
Solved Threads: 0
Willco Willco is offline Offline
Newbie Poster

Re: Console application colors?

 
0
  #6
Mar 31st, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: jazu100 is an unknown quantity at this point 
Solved Threads: 0
jazu100's Avatar
jazu100 jazu100 is offline Offline
Newbie Poster

Re: Console application colors?

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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,628
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Console application colors?

 
0
  #8
Mar 31st, 2009
Originally Posted by Willco View Post
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.
  1. #include <iostream>
  2. int main()
  3. {
  4. std::cout << "Hello World\n";
  5. }

Originally Posted by Willco View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 320
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Console application colors?

 
0
  #9
Mar 31st, 2009
Originally Posted by Willco View Post
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.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 320
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Console application colors?

 
0
  #10
Mar 31st, 2009
for me this is only the code space that taken to that 5 lines long "Hello World" application.

  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
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC