Im dos fenster farbigen text ?



  • tara schrieb:

    Hallo ich wollte mal fragen wie kann ich ihm dos fenster Das "Hallo Welt" farbig darstellen ? ich hätte dies gerne ihn gelb kann mir da einer helfen oder ein beispiel geben ? 🙂

    Welches OS?
    Bei Windows könnte Sidewinders Improved Console recht interessant sein.

    Caipi



  • Ich hab dazu mal vor langer langer Zeit mal ne Klasse geschrieben.

    Ob und wie sie funktioniert kann ich dir im mom nicht erzählen.
    Sie funktioniert auch nur unter Win.

    #ifndef __MYCONSOLE_H__
    #define __MYCONSOLE_H__
    
    #include <stdio.h>
    #include <windows.h>
    
    class Console
    {
    private:
    	static HANDLE hStdout;
    	static CONSOLE_SCREEN_BUFFER_INFO csbi;
    
    public:
    
    	Console();
    	~Console();
    	void	gotoXY		(int x, int y);
    	void	delLine		();
    	void	fillRect	(int startx, int starty, int stopx, int stopy, char zeichen);
    	void	clrScreen	();
    	int		getX		();
    	int		getY		();
    	void	setText		(char* foreground, char* background);
    	void	resetText	();
    	void	setTitle	(char* title);
    
    };
    
    #endif // #define __MYCONSOLE_H__
    
    /* EOF */
    
    #ifndef __MYCONSOLE_CPP__
    #define __MYCONSOLE_CPP__
    
    #include "console.h"
    
    /*
    Screen:
    +----------------------------------------+
    |0,0								 79,0|
    |										 |
    |										 |
    |										 |
    |										 |
    |										 |
    |0,24								79,24|
    +----------------------------------------+
    */
    
    /*
    Method:			Console
    Param:			void
    Return:			void
    Description:	Default constructor
    				It initializes the screen settings
    */
    Console::Console()
    {
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    	GetConsoleScreenBufferInfo(hStdout,&csbi);
    }
    /* 
    Static vars
    */ 
    HANDLE Console::hStdout;
    CONSOLE_SCREEN_BUFFER_INFO Console::csbi;
    
    /*
    Method:			~Console
    Param:			void
    Return:			void
    Description:	Default destructor
    */
    Console::~Console()
    {
    }
    
    /*
    Method:			gotoXY
    Param:			int x - the x coordinate
    				int y - the Y coordinate
    Return:			void
    Description:	Place the cursor on the transmitted coordinates
    */
    void Console::gotoXY (int x, int y)
    {
    	COORD pos;
    
    	if (x < 0)
    	{
    		x = 0;
    	}
    	if (x > 79)
    	{
    		x = 79;
    	}
    	if (y < 0)
    	{
    		y = 0;
    	}
    	if (y > 24)
    	{
    		y = 24;
    	}
    
    	pos.X = x;
    	pos.Y = y;
    
    	SetConsoleCursorPosition(hStdout,pos);
    }
    
    /*
    Method:			delLine
    Param:			void
    Return:			void
    Description:	Clears the whole current line
    */
    void Console::delLine ()
    {
    	COORD pos;
    	DWORD n;
    	DWORD w;
    
    	pos.X = 0;
    	pos.Y = csbi.dwCursorPosition.Y;
    	n = csbi.dwSize.X;
    
    	FillConsoleOutputCharacter(hStdout,' ',n,pos,&w);
    }
    
    /*
    Method:			fillRect
    Param:			int startx	- The x-position where you want to begin the rectangle
    				int starty	- The y-position where you want to begin the rectangle
    				int stopx	- The x-position where you want to end the rectangle
    				int stopy	- The y-position where you want to end the rectangle
    				char fill	- The character that will be filled into the rectangle
    Return:			void
    Description:	Print a rectangle
    */
    
    void Console::fillRect (int startx, int starty, int stopx, int stopy, char zeichen)
    {
    	COORD pos;
    	int diffx = 0;
    	int diffy = 0;
    	int tmp = 0;
    	int x, y;
    
    	if (startx > stopx)
    	{
    		tmp = startx;
    		startx = stopx;
    		stopx = tmp;
    	}
    	if (starty > stopy)
    	{
    		tmp = starty;
    		starty = stopy;
    		stopy = tmp;
    	}
    
    	pos.X = startx;
    	pos.Y = starty;
    
    	diffx = stopx - startx;
    	diffy = stopy - starty;
    
    	gotoXY (pos.X, pos.Y);
    
    	for (y = 0; y <= diffy; y++)			// Zeile
    	{
    		for (x = 0; x <= diffx; x++)		// Spalte
    		{
    			gotoXY (pos.X + x, pos.Y + y);
    			printf ("%c", zeichen);
    		}
    	}
    
    }
    
    /*
    Method:			clrScreen
    Param:			void
    Return:			void
    Description:	Print a rectangle from (0,0) to (79,24)
    				The effect is, that the visible screen is deleted
    */
    void Console::clrScreen ()
    {
    	fillRect(0, 0, 79, 24, ' ');
    	gotoXY (0, 0);
    }
    
    /*
    Method:			getX
    Param:			void
    Return:			int
    Description:	Returns the actual X-axis cursor position
    */
    int Console::getX ()
    {
    	return (csbi.dwCursorPosition.X);
    }
    
    /*
    Method:			getY
    Param:			void
    Return:			int
    Description:	Returns the actual Y-axis cursor position
    */
    int Console::getY ()
    {
    	return (csbi.dwCursorPosition.Y);
    }
    
    /*
    Method:			setText
    Param:			char* foreground - defines the foreground color
    				char* background - defines the background color
    				Possible colors:	0 BLACK
    									1 BLUE
    									2 GREEN
    									3 CYAN
    									4 RED
    									5 PINK
    									6 GOLD
    									7 GREY
    									8 DARKGREY
    									9 IBLUE
    									10 IGREEN
    									11 ICYAN
    									12 IRED
    									13 IPINK
    									14 YELLOW
    									15 WHITE
    Return:			void
    Description:	Set the fore and background color of the console.
    */
    void Console::setText (char* foreground, char* background)
    {
    	unsigned short setting = 0;
    	unsigned short setting2 = 0;
    	unsigned short final = 0;
    
    	if (strcmp(foreground, "BLACK") == 0)
    		setting = 0;
    	else if (strcmp(foreground, "BLUE") == 0)
    		setting = 1;
    	else if (strcmp(foreground, "GREEN") == 0)
    		setting = 2;
    	else if (strcmp(foreground, "CYAN") == 0)
    		setting = 3;
    	else if (strcmp(foreground, "RED") == 0)
    		setting = 4;
    	else if (strcmp(foreground, "PINK") == 0)
    		setting = 5;
    	else if (strcmp(foreground, "GOLD") == 0)
    		setting = 6;
    	else if (strcmp(foreground, "GREY") == 0)
    		setting = 7;
    	else if (strcmp(foreground, "DARKGREY") == 0)
    		setting = 8;
    	else if (strcmp(foreground, "IBLUE") == 0)
    		setting = 9;
    	else if (strcmp(foreground, "IGREEN") == 0)
    		setting = 10;
    	else if (strcmp(foreground, "ICYAN") == 0)
    		setting = 11;
    	else if (strcmp(foreground, "IRED") == 0)
    		setting = 12;
    	else if (strcmp(foreground, "IPINK") == 0)
    		setting = 13;
    	else if (strcmp(foreground, "YELLOW") == 0)
    		setting = 14;
    	else if (strcmp(foreground, "WHITE") == 0)
    		setting = 15;
    	else
    		setting = 7;
    
    	if (strcmp(background, "BLACK") == 0)
    		setting2 = 0;
    	else if (strcmp(background, "BLUE") == 0)
    		setting2 = 1;
    	else if (strcmp(background, "GREEN") == 0)
    		setting2 = 2;
    	else if (strcmp(background, "CYAN") == 0)
    		setting2 = 3;
    	else if (strcmp(background, "RED") == 0)
    		setting2 = 4;
    	else if (strcmp(background, "PINK") == 0)
    		setting2 = 5;
    	else if (strcmp(background, "GOLD") == 0)
    		setting2 = 6;
    	else if (strcmp(background, "GREY") == 0)
    		setting2 = 7;
    	else if (strcmp(background, "DARKGREY") == 0)
    		setting2 = 8;
    	else if (strcmp(background, "IBLUE") == 0)
    		setting2 = 9;
    	else if (strcmp(background, "IGREEN") == 0)
    		setting2 = 10;
    	else if (strcmp(background, "ICYAN") == 0)
    		setting2 = 11;
    	else if (strcmp(background, "IRED") == 0)
    		setting2 = 12;
    	else if (strcmp(background, "IPINK") == 0)
    		setting2 = 13;
    	else if (strcmp(background, "YELLOW") == 0)
    		setting2 = 14;
    	else if (strcmp(background, "WHITE") == 0)
    		setting2 = 15;
    	else
    		setting2 = 0;
    
    	setting2 <<= 4;
    
    	final = setting2 | setting;
    
    	SetConsoleTextAttribute(hStdout,final);
    }
    
    /*
    Method:			resetText
    Param:			void
    Return:			void
    Description:	The original DOS Style will be resetted
    */
    void Console::resetText ()
    {
    	SetConsoleTextAttribute(hStdout,7);
    }
    
    /*
    Method:			setTitle
    Param:			char - the string which should be displayed
    Return:			void
    Description:	This method sets the title of the console window
    */
    void Console::setTitle(char* title)
    {
    	SetConsoleTitle(title);
    }
    
    #endif // #define __MYCONSOLE_CPP__
    
    /* EOF */
    


  • das steht alles im konsolenfaq.



  • @VC: Bin ja Fan von Eigenwerbung, aber wenn du dir mal die Improved Console angesehen hättest, wäre dir aufgefallen, dass das ebenfalls eine solche Console-Klasse ist + viel viel besser implementiert (dein setText() ist grauenhaft) + viel mehr Funktionalität.

    /Edit: Dein clrScreen() schlag ich vom Speed her ja schon fast mit system("cls")... 🙂

    MfG SideWinder



  • Vic meinst du nicht das das evtl. ein bischen umständlich ist?

    ich würds so machen:
    Komplette Konsole in Farbe:

    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    { 
         system("color 40");
         cout << "Hello World!" << endl;
    
         getch();
         retun 0;
    }
    

    Einzelne Textabschnitte (bzw. nur den Text der nach dem Befehl kommt):

    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    #define gelb 0x0040 //Farbe Deffinieren
    
    void farbe(WORD color) {
        SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
    }
    
    int main()
    { 
         farbe(gelb);
         cout << "Hello World!" << endl;
    
         getch();
         retun 0;
    }
    


  • @LordSash: Deine zwei Möglichkeiten haben das selbe Ergebnis, es ist nicht der Fall, dass bei Funktion 2 nur die nächste Ausgabe in dieser Farbe ausgegeben wird. Dein erste Möglichkeit ist mit Abstand die schlechteste Möglichkeit die Farbe umzustellen. Die zweite Möglichkeit findest du so auch in der IC - nur mit besserem Environment und keinen Makros für die Farben.

    MfG SideWinder



  • Hat mir alles gut gefallen 🙂 also echt danke ich glaube ich werde mich hier mal regiestrieren denn ich habe noch mehr fragen 🙂



  • @MartinMilbret
    Kalr das währe echt net wie ist deine icq nummer ? meine bzw meim bruder seine (sitze an seinem pc) ist 237-882-603
    freue mich immer wenn mir einer helfen will



  • Okay hab dich geaddet(VIRUS) und jetzt weiss ich ja mal was ich tun muss(konsolen FAQ, c++ FAQ und falls was fehlt bitte posten.
    Bin der erstmal FAQs lesen 😉



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum DOS und Win32-Konsole verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten