++C מסך צבעוני - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

++C מסך צבעוני


~שירה

Recommended Posts

תראו מה מצאתי בתיקייה שכוחת אל אחת שיש לי על המחשב :)

#ifndef _Console_H_
#define _Console_H_

//////////////////////////////////////////////////////////////////
//
// A simple win32 Console wrapper class that facilitates
// clearing the screen, going to a specific screen position
// and seting screen colors.
// Future updates:
// - create/destroy a new console window
// - clear a specific rectangle with a character/attribute
// (good for making text windows)
// - whatever else I think of
//
// Author: _Dimi
// Oct 20 2000 - Created

class Console
{
public:

// Wrap a given console (hCout be the output handle) or
// get the handle to the current
Console(HANDLE hCout = NULL)
{
if (hCout == NULL)
{
hCout = GetStdHandle(STD_OUTPUT_HANDLE);
}
m_hCout = hCout;
}

// Goto a screen position defined by (row, col)
void GoTo(UINT row, UINT col)
{
if (m_hCout != NULL)
{
COORD c;
c.X = col;
c.Y = row;
SetConsoleCursorPosition(m_hCout, c);
}
}

// Set the text color. Range is 0(black) .. 14(bright white)
void SetTextColor(UINT c)
{
if (m_hCout != NULL)
{
WORD attr = 0;
switch(c)
{
case 1: attr = FOREGROUND_RED; break;
case 2: attr = FOREGROUND_GREEN; break;
case 3: attr = FOREGROUND_BLUE; break;
case 4: attr = FOREGROUND_RED | FOREGROUND_GREEN; break;
case 5: attr = FOREGROUND_RED | FOREGROUND_BLUE; break;
case 6: attr = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
case 7: attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;

case 8: attr = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
case 9: attr = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case 10: attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case 11: attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
case 12: attr = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case 13: attr = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
case 14: attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
}
m_ForeGround = attr;
SetConsoleTextAttribute(m_hCout, m_ForeGround | m_BackGround);
}
}

// Set the background color. Range is 0(black) .. 14(bright white)
void SetBkColor(UINT c)
{
if (m_hCout != NULL)
{
WORD attr = 0;
switch(c)
{
case 1: attr = BACKGROUND_RED; break;
case 2: attr = BACKGROUND_GREEN; break;
case 3: attr = BACKGROUND_BLUE; break;
case 4: attr = BACKGROUND_RED | BACKGROUND_GREEN; break;
case 5: attr = BACKGROUND_RED | BACKGROUND_BLUE; break;
case 6: attr = BACKGROUND_GREEN | BACKGROUND_BLUE; break;
case 7: attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; break;

case 8: attr = BACKGROUND_RED | BACKGROUND_INTENSITY; break;
case 9: attr = BACKGROUND_GREEN | BACKGROUND_INTENSITY; break;
case 10: attr = BACKGROUND_BLUE | BACKGROUND_INTENSITY; break;
case 11: attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY; break;
case 12: attr = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY; break;
case 13: attr = BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY; break;
case 14: attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY; break;
}
m_BackGround = attr;
SetConsoleTextAttribute(m_hCout, m_ForeGround | m_BackGround);
}
}

// Clear the screen using the currently set attributes and
// the given fill character (default = space).
void Clear(TCHAR ch = ' ')
{
if (m_hCout != NULL)
{
COORD c = {0,0};
DWORD len;
FillConsoleOutputAttribute(m_hCout, m_ForeGround | m_BackGround, 100000, c, &len);
FillConsoleOutputCharacter(m_hCout, ch, len, c, &len);
}
}

private:
HANDLE m_hCout; // the console handle
WORD m_ForeGround; // the current foreground attribute
WORD m_BackGround; // the current background attribute
};

#endif

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...