Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/// File: console.h #if !defined _CON_H_ #define _CON_H_ #include <windows.h> #include <iostream> // written by Grek40, Jens Grünberg (koschka) // Version 1a namespace Console { // Cursortypen namespace Cursor { enum CursorType { SOLID_CURSOR = 100, NORMAL_CURSOR = 25, SMALL_CURSOR = 5 }; } // Vorgefertigte Fraben fuer die Konsole namespace Color { enum COLORS { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE }; // Erstellt ein Farbattribut, welches aus Vorderundfarbe und Hintergrundfarbe besteht. // Die Funktion Console::attribute(...) braucht ein solches Attribute und setzt Vordergrund und // Hintergrund unsigned createColorAttribute(unsigned foregroundColor, unsigned backgroundColor); } unsigned static _BackgroundColor = Color::BLACK; unsigned static _ForegroundColor = Color::WHITE; // zu (x, y) Koordinate gehen void gotoxy( unsigned short x, unsigned short y ); // Farben (SetConsoleTextAttribute) void attribute( unsigned short attr = 0 ); // Cursortyp void cursor(Cursor::CursorType cursor, bool visible = true ); // Konsolenfenster void displaysize( unsigned short _X, unsigned short _Y ); // Ausgabebuffer void buffersize( unsigned short _X, unsigned short _Y ); // Sets text background color. void textcolor(unsigned color); // Sets text foreground color void textbackground(unsigned color); // Titel setzen inline void title( const char* title ) { if ( ! SetConsoleTitleA( title ) ) { /*Fehlerbehandlung*/ } } // Clear Screen void cls(void); } #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
/// includes File: console.h #include "console.h" namespace Console { void gotoxy( unsigned short x, unsigned short y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD pos = { x, y }; if ( !::SetConsoleCursorPosition( hCon, pos ) ) { /* Platz für individuelle Fehlerbehandlung ^^*/ } } // Farben gibts hier void attribute( unsigned short attr ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); _BackgroundColor = attr >> 4; _ForegroundColor = attr; if ( ! SetConsoleTextAttribute( hCon, attr ) ) { // Platz für individuelle Fehlerbehandlung^^ } } // Cursorgröße, -sichtbarkeit void cursor(Cursor::CursorType cursor, bool visible ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_CURSOR_INFO info; if ( cursor < static_cast<Cursor::CursorType>(1) ) { cursor = Cursor::SMALL_CURSOR; visible = false; } info.bVisible = visible; info.dwSize = static_cast<DWORD>( cursor ); if ( ! SetConsoleCursorInfo( hCon, &info ) ) { // Platz für individuelle Fehlerbehandlung^^ } } // Hier wird das Konsolenfenster bearbeitet void displaysize( unsigned short _X, unsigned short _Y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO info; SMALL_RECT change = {0}; if ( ! GetConsoleScreenBufferInfo( hCon, &info ) ) { // Platz für individuelle Fehlerbehandlung^^ } if ( _X >= info.dwMaximumWindowSize.X ) change.Right = info.dwMaximumWindowSize.X-1; else change.Right = _X; if ( _Y >= info.dwMaximumWindowSize.Y ) change.Bottom = info.dwMaximumWindowSize.Y-1; else change.Bottom = _Y; if ( ! SetConsoleWindowInfo( hCon, true, &change ) ) { // Platz für individuelle Fehlerbehandlung^^ } } // Jetzt ist der Ausgabebuffer dran void buffersize( unsigned short _X, unsigned short _Y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD newSize; newSize.X = _X; newSize.Y = _Y; if ( ! SetConsoleScreenBufferSize( hCon, newSize ) ) { // Platz für individuelle Fehlerbehandlung^^ } } // So oft neue Zeile, bis der Buffer wieder schön sauber ist //FillConsoleOutputCharacter(...) macht leider blöde Effekte bei Hintergrundfarben void cls(void) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO info; if ( ! GetConsoleScreenBufferInfo( hCon, &info ) ) { // Platz für individuelle Fehlerbehandlung^^ } wchar_t* fill = new wchar_t[info.dwSize.Y+1]; for (int i=0; i<=info.dwSize.Y; ++i) fill[i] = L'\n'; fill[info.dwSize.Y] = 0; gotoxy(0, info.dwSize.Y-1); std::wcout << fill; gotoxy(0, 0); delete [] fill; } // Farbattribut setzen unsigned Color::createColorAttribute(unsigned foregroundColor, unsigned backgroundColor) { return (backgroundColor << 4) | foregroundColor; } // Textfarbe setzen void textcolor(unsigned color) { _ForegroundColor = color; attribute(Color::createColorAttribute(color, _BackgroundColor)); } // Hintergrundfarbe setzen void textbackground(unsigned color) { _BackgroundColor = color; attribute(Color::createColorAttribute(_ForegroundColor, color)); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include "console.h" #include <iostream> int main(void) { // vollen cursor bitte Console::cursor(Console::Cursor::SOLID_CURSOR); // Textfarbe blau und Hintergrund Weiß Console::attribute(Console::Color::createColorAttribute(Console::Color::LIGHTBLUE, Console::Color::WHITE)); // Bildschirm löschen, dabei wird dieser auch weiß angestrichen Console::cls(); // gehe an die Position (x:3, y:2) Console::gotoxy(3, 2); std::cout << " blue on white "; Console::textcolor(Console::Color::GREEN); std::cout << " green on white "; // warten std::cin.ignore(); std::cin.get(); return 0; } |