Win32 Console Application Tools
Posted
by Roger McElfresh
on May 23rd, 2002
Environment: VC6, SP5, WinXP; should also work on Win9x
- void clrscr();
- void gotoxy(x,y);
- void setrgb(color);
The code to accomplish this follows.
// console.cpp // #include "console.h" using namespace std; // // Clears the screen // void clrscr() { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi;); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } // // Moves the cursor to x, y in console window // ie x=left\right y=top\bottom // void gotoxy(int x, int y) { COORD point; point.X = x; point.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); } // // Set text and background colors // void setrgb(int color) { switch (color) { case 0: // White on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case 1: // Red on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); break; case 2: // Green on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case 3: // Yellow on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case 4: // Blue on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case 5: // Magenta on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; case 6: // Cyan on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case 7: // Black on Gray SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | BACKGROUND_INTENSITY); break; case 8: // Black on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); break; case 9: // Red on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED); break; case 10: // Green on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_GREEN); break; case 11: // Yellow on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN); break; case 12: // Blue on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_BLUE); break; case 13: // Magenta on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_BLUE); break; case 14: // Cyan on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case 15: // White on White SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; default : // White on Black SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; } }
Downloads
Download demo project - 110 KbDownload source - 7 Kb







Comments
is possible this method??
Posted by Legacy on 02/11/2004 12:00amOriginally posted by: pegas
i know console api.
i wanna execute the ConsoleApp and send the data.
flow chart is below..
main program is windows application.
in main program, createprocess ConsoleApp(exe)
then, i wanna control the consoleapp in main program.
But i cant get the handle of ConsoleApp..
How Method can i...
ReplyTroubles !
Posted by Legacy on 01/20/2004 12:00amOriginally posted by: MTJ
ReplyDOS-Appearance
Posted by Legacy on 12/12/2003 12:00amOriginally posted by: Alon Weiss
Replymay i ask something??
Posted by Legacy on 11/14/2003 12:00amOriginally posted by: blue-bon
i had try the code n it's work...thx
but somthing bothering me that i dont know what does its statement mean. esp. -SetConsoleTextAtribute-. any one can help me to find out the description for this syntax....
Replythx
Example on Drawing w/the gotoxy function
Posted by Legacy on 09/01/2003 12:00amOriginally posted by: Wes Baldwin
Replysizing the console window other than default 80X30
Posted by Legacy on 08/11/2003 12:00amOriginally posted by: Adam
What's the most straighforward way to size the console window programmatically so the code determines the console size when the program is launched.
ReplyYour DEFAULT gray isn't the real DOS-gray
Posted by Legacy on 08/11/2003 12:00amOriginally posted by: Lord of the Things
How can i get back that fine DOS-Gray after using these funcs?
Reply(Cause your default isn't exactly the same)
Very Kewl
Posted by Legacy on 06/07/2003 12:00amOriginally posted by: Retief
I just poped in your function for font color into my program..added the windows.h and pow! works great
thanks a lot
ReplyCan I minimize or hide a console programmatically as well?
Posted by Legacy on 03/11/2003 12:00amOriginally posted by: SGL
I would like to minimize (at least) my console app, but don't see a function which allows this. Any ideas?
Thanks,
ReplySteve
If you want to use this in VC++ 5.0...
Posted by Legacy on 03/11/2003 12:00amOriginally posted by: Dan
A friend and I are sitting here in class and he wanted to use your color thing to create a line of characters in his program that flickers different colors. The problem is, whatever was the last color he chose, the compiler made the entire thing that color. We found though that this problem is solved - so far as we can tell - by using 'printf' instead of 'cout'. I am posting here to let any other VC++ 5.0 users know this, but also to see if anyone has an answer as to why the compiler did this.
ReplyLoading, Please Wait ...