kleuren in een dosvenster

Status
Niet open voor verdere reacties.

tchembado

Gebruiker
Lid geworden
28 nov 2006
Berichten
93
ik zou wat kleur wille brengen in een van mijn progjes in dos.
Hoe kan ik dat het gemakelijste doen?
Ik heb het dan over:

-andere tekst kleur
-andere achtergrond kleur

alvast bedankt,

Tchembado
 
Dat is niet zo makkelijk zelf te schrijven, ik heb hier wel nog een header liggen die dat doet voor windowsconsoles. Als je op *nix/linux/mac werkt zal dit niet zo veel helpen denkik...
--Johan
 
die header is goed genoeg voormij :)

ik werk op vista nu.

alvast bedankt,

groetjes,
Tchembado
 
Hier heb je wat moois:

Code:
enum Colors { blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };

void coutc(int color, char* output)
{
   HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute( handle,color);
   cout<< output;
   SetConsoleTextAttribute( handle, color);
}

Nu heb je een functie die je je vanuit je programma kunt aanroepen:

Code:
coutc(blue, "test");

Vergeet niet windows.h te includen!

gr Niek
 
Hier heb je de header (kleur.h)
Code:
/*************************************************/
/* kleur.h & kleur.cpp                           */
/* created by: Johan Temmerman                   */
/*         based on other code-snippets          */
/*                                               */
/* Once a color is set, it remains that way till */
/* anotherone is set, or till the end of your    */
/* program. The appearance of your console won't */
/* be affected in other console-programs         */
/*                                               */
/* Use: include this header and you can use the  */
/*      inline defined commands in your program  */
/*      like this:                               */
/*                                               */
/*      std::cout << clr::fg_red << "red";       */
/*                                               */
/* The 'fg_'-prefix means you're changing the    */
/* color of the text. The 'bg_'-names apply to   */
/* the background of the console.                */
/*                                               */
/*   The inline-command names are the original   */
/*   16 VGA-names of the colors:                 */
/*   yellow, fuchsia, red, aqua, lime, blue      */
/*   olive, purple, maroon, teal, green, navy    */
/*   white, silver, grey, black                  */
/*                                               */
/*************************************************/

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

#ifndef K_L_E_U_R__H
#define K_L_E_U_R__H

namespace clr
{
    //huidige kleuren
    static const WORD bgMask( BACKGROUND_BLUE      | 
                              BACKGROUND_GREEN     | 
                              BACKGROUND_RED       | 
                              BACKGROUND_INTENSITY   );
    static const WORD fgMask( FOREGROUND_BLUE      | 
                              FOREGROUND_GREEN     | 
                              FOREGROUND_RED       | 
                              FOREGROUND_INTENSITY   );
    //tekstkleur
    static const WORD fgBlack    ( 0 ); 
    static const WORD fgLoRed    ( FOREGROUND_RED   ); 
    static const WORD fgLoGreen  ( FOREGROUND_GREEN ); 
    static const WORD fgLoBlue   ( FOREGROUND_BLUE  ); 
    static const WORD fgLoCyan   ( fgLoGreen   | fgLoBlue ); 
    static const WORD fgLoMagenta( fgLoRed     | fgLoBlue ); 
    static const WORD fgLoYellow ( fgLoRed     | fgLoGreen ); 
    static const WORD fgLoWhite  ( fgLoRed     | fgLoGreen | fgLoBlue ); 
    static const WORD fgGrey     ( fgBlack     | FOREGROUND_INTENSITY );
    static const WORD fgHiWhite  ( fgLoWhite   | FOREGROUND_INTENSITY ); 
    static const WORD fgHiBlue   ( fgLoBlue    | FOREGROUND_INTENSITY ); 
    static const WORD fgHiGreen  ( fgLoGreen   | FOREGROUND_INTENSITY ); 
    static const WORD fgHiRed    ( fgLoRed     | FOREGROUND_INTENSITY ); 
    static const WORD fgHiCyan   ( fgLoCyan    | FOREGROUND_INTENSITY ); 
    static const WORD fgHiMagenta( fgLoMagenta | FOREGROUND_INTENSITY ); 
    static const WORD fgHiYellow ( fgLoYellow  | FOREGROUND_INTENSITY );
    //achtergrond
    static const WORD bgBlack    ( 0 ); 
    static const WORD bgLoRed    ( BACKGROUND_RED   ); 
    static const WORD bgLoGreen  ( BACKGROUND_GREEN ); 
    static const WORD bgLoBlue   ( BACKGROUND_BLUE  ); 
    static const WORD bgLoCyan   ( bgLoGreen   | bgLoBlue ); 
    static const WORD bgLoMagenta( bgLoRed     | bgLoBlue ); 
    static const WORD bgLoYellow ( bgLoRed     | bgLoGreen ); 
    static const WORD bgLoWhite  ( bgLoRed     | bgLoGreen | bgLoBlue ); 
    static const WORD bgGrey     ( bgBlack     | BACKGROUND_INTENSITY );
    static const WORD bgHiWhite  ( bgLoWhite   | BACKGROUND_INTENSITY ); 
    static const WORD bgHiBlue   ( bgLoBlue    | BACKGROUND_INTENSITY ); 
    static const WORD bgHiGreen  ( bgLoGreen   | BACKGROUND_INTENSITY ); 
    static const WORD bgHiRed    ( bgLoRed     | BACKGROUND_INTENSITY ); 
    static const WORD bgHiCyan   ( bgLoCyan    | BACKGROUND_INTENSITY ); 
    static const WORD bgHiMagenta( bgLoMagenta | BACKGROUND_INTENSITY ); 
    static const WORD bgHiYellow ( bgLoYellow  | BACKGROUND_INTENSITY );
    
    //klasse om info over de console op te halen en zo aan te passen
    static class Kleur
    {
      private:
          HANDLE                      hCon;            //handle van de console
          DWORD                       cCharsWritten; 
          CONSOLE_SCREEN_BUFFER_INFO  csbi; 
          DWORD                       dwConSize;
          
          void GetInfo();

      public:
          Kleur();
          void SetColor( WORD wRGBI, WORD Mask );
    } console;
    
    ///////////////////////////////////////////////////
    // vereenvoudigde aanroep voor kleurwissel maken //
    ///////////////////////////////////////////////////
    
    //tekstkleuren
    inline std::ostream& fg_yellow( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiYellow, bgMask );
        return os;
    }
    inline std::ostream& fg_fuchsia( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiMagenta, bgMask );
        return os;
    }
    inline std::ostream& fg_red( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiRed, bgMask );
        return os;
    }
    inline std::ostream& fg_aqua( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiCyan, bgMask );
        return os;
    }
    inline std::ostream& fg_lime( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiGreen, bgMask );
        return os;
    }
    inline std::ostream& fg_blue( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiBlue, bgMask );
        return os;
    }

    inline std::ostream& fg_olive( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoYellow, bgMask );
        return os;
    }
    inline std::ostream& fg_purple( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoMagenta, bgMask );
        return os;
    }
    inline std::ostream& fg_maroon( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoRed, bgMask );
        return os;
    }
    inline std::ostream& fg_teal( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoCyan, bgMask );
        return os;
    }
    inline std::ostream& fg_green( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoGreen, bgMask );
        return os;
    }
    inline std::ostream& fg_navy( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoBlue, bgMask );
        return os;
    }

    inline std::ostream& fg_white( std::ostream& os ){
        os.flush();
        console.SetColor( fgHiWhite, bgMask );
        return os;
    }
    inline std::ostream& fg_silver( std::ostream& os ){
        os.flush();
        console.SetColor( fgLoWhite, bgMask );
        return os;
    }
    inline std::ostream& fg_grey( std::ostream& os ){
        os.flush();
        console.SetColor( fgGrey, bgMask );
        return os;
    }
    inline std::ostream& fg_black( std::ostream& os ){
        os.flush();
        console.SetColor( fgBlack, bgMask );
        return os;
    }

    //achtergrondkleuren
    inline std::ostream& bg_yellow( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiYellow, fgMask );
        return os;
    }
    inline std::ostream& bg_fuchsia( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiMagenta, fgMask );
        return os;
    }
    inline std::ostream& bg_red( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiRed, fgMask );
        return os;
    }
    inline std::ostream& bg_aqua( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiCyan, fgMask );
        return os;
    }
    inline std::ostream& bg_lime( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiGreen, fgMask );
        return os;
    }
    inline std::ostream& bg_blue( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiBlue, fgMask );
        return os;
    }

    inline std::ostream& bg_olive( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoYellow, fgMask );
        return os;
    }
    inline std::ostream& bg_purple( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoMagenta, fgMask );
        return os;
    }
    inline std::ostream& bg_maroon( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoRed, fgMask );
        return os;
    }
    inline std::ostream& bg_teal( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoCyan, fgMask );
        return os;
    }
    inline std::ostream& bg_green( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoGreen, fgMask );
        return os;
    }
    inline std::ostream& bg_navy( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoBlue, fgMask );
        return os;
    }

    inline std::ostream& bg_white( std::ostream& os ){
        os.flush();
        console.SetColor( bgHiWhite, fgMask );
        return os;
    }
    inline std::ostream& bg_silver( std::ostream& os ){
        os.flush();
        console.SetColor( bgLoWhite, fgMask );
        return os;
    }
    inline std::ostream& bg_grey( std::ostream& os ){
        os.flush();
        console.SetColor( bgGrey, fgMask );
        return os;
    }
    inline std::ostream& bg_black( std::ostream& os ){
        os.flush();
        console.SetColor( bgBlack, fgMask );
        return os;
    }
}

#endif //K_L_E_U_R__H
En hier is de bijhorende .cpp-file van de klasse (kleur.cpp):
Code:
#include "kleur.h"
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace clr;
   
/////////////
// PRIVATE //
/////////////

void Kleur::GetInfo(){   //info over de console opvragen
   GetConsoleScreenBufferInfo( hCon, &csbi );
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;   //afmetingen
}

////////////
// PUBLIC //
////////////

Kleur::Kleur(){    //constructor zorgt ervoor dat de handle van het consolevenster wordt toegekend
   hCon = GetStdHandle( STD_OUTPUT_HANDLE );
}

void Kleur::SetColor( WORD wRGBI, WORD Mask ){   //kleur instellen
   GetInfo();
   csbi.wAttributes &= Mask;
   csbi.wAttributes |= wRGBI; 
   SetConsoleTextAttribute( hCon, csbi.wAttributes );
}

Voor gebruik, zie de commentaar in de header.
 
Je kan zo'n programma ook schrijven in batch en dat dan aanroepen via system("[file]");
om een lijst van alle kleuren te hebben tip je help color in de commando prompt
Om bijvoorbeeld groene letters op een zwarte achtergrond te hebben:

colors.bat:
@color 0A

main.cpp:
int main()
{
system("colors.bat");
}
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan