Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Autor : Tiago Portela
- Email : sapitando@gmail.com
- Sobre : Compilado com TDM-GCC 5.10 64 bit. Funções para console Windows.
- Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
- #ifndef _WINDOWS_
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #endif
- #ifndef _INC_STDIO
- #include <stdio.h>
- #endif
- #ifndef _INC_STDLIB
- #include <stdlib.h>
- #endif
- // F_ means foreground.
- #define F_BLACK 0
- #define F_BLUE 1
- #define F_GREEN 2
- #define F_CYAN 3
- #define F_RED 4
- #define F_MAGENT 5
- #define F_YELLOW 6
- #define F_WHITE 7
- #define F_LIGHT 8
- #define F_LIGHTBLUE 9
- #define F_LIGHTGREEN 10
- #define F_LIGHTCYAN 11
- #define F_LIGHTRED 12
- #define F_LIGHTMAGENT 13
- #define F_LIGHTYELLOW 14
- #define F_LIGHTWHITE 15
- // B_ means background.
- #define B_BLACK 0
- #define B_BLUE 16
- #define B_GREEN 32
- #define B_CYAN 48
- #define B_RED 64
- #define B_MAGENT 80
- #define B_YELLOW 96
- #define B_WHITE 112
- #define B_LIGHT 128
- #define B_LIGHTBLUE 144
- #define B_LIGHTGREEN 160
- #define B_LIGHTCYAN 176
- #define B_LIGHTRED 192
- #define B_LIGHTMAGENT 208
- #define B_LIGHTYELLOW 224
- #define B_LIGHTWHITE 240
- // Current color.
- #define ATTR (sFBColors.wAttributes)
- // Current foreground color.
- #define F_ATTR (sFBColors.Color.Foreground)
- // Current background color.
- #define B_ATTR (sFBColors.wAttributes & 240)
- // Reverse background and foreground colors.
- #define REVERSE ((sFBColors.Color.Foreground << 4) | (sFBColors.Color.Background))
- // Intern use.
- #define CLEAR_LEFT_TO_RIGHT 1
- #define CLEAR_TOP_TO_BOTTOM 2
- #define INSERT_LEFT_TO_RIGHT 3
- #define INSERT_TOP_TO_BOTTOM 4
- #define INSERT_RIGHT_TO_LEFT 5
- #define INSERT_BOTTOM_TO_TOP 6
- #define REPLACE_LEFT_TO_RIGHT 7
- #define REPLACE_TOP_TO_BOTTOM 8
- typedef struct _WindowInfo {
- COORD Min;
- COORD Max;
- COORD Size;
- } Window_t;
- typedef struct _ScreenInfo {
- Window_t Window;
- Window_t Boundary;
- } Screen_t;
- typedef struct _Color {
- DWORD Foreground : 4;
- DWORD Background : 4;
- DWORD : 24;
- } Color_t;
- typedef union _ForegroundBackgroundColor {
- DWORD wAttributes;
- Color_t Color;
- } FBColor_t;
- HANDLE hConsoleOutput = NULL;
- Window_t sWindow = {{0,0},{0,0},{0,0}};
- Screen_t sScreen = {{{0,0},{0,0},{0,0}},{{0,0},{0,0},{0,0}}};
- FBColor_t sFBColors = {.wAttributes = 7};
- BOOL _iOK = FALSE;
- // Intern use.
- void _Initialize(void);
- COORD _Where(void);
- void _Block(const Window_t sWind_,
- const UCHAR cHowInsert,
- const SHORT iHowMany,
- const DWORD wAttributes,
- const UCHAR ucChar);
- // Functions.
- SHORT WhereWindowX(void)
- {
- return (_Where().X - sWindow.Min.X) + 1;
- }
- SHORT WhereWindowY(void)
- {
- return (_Where().Y - sWindow.Min.Y) + 1;
- }
- SHORT WhereScreenX(void)
- {
- return _Where().X + 1;
- }
- SHORT WhereScreenY(void)
- {
- return _Where().Y + 1;
- }
- SHORT WindowMaxX(void)
- {
- if(!_iOK) {_Initialize();}
- return sWindow.Size.X;
- }
- SHORT WindowMaxY(void)
- {
- if(!_iOK) {_Initialize();}
- return sWindow.Size.Y;
- }
- SHORT ScreenMaxX(void)
- {
- if(!_iOK) {_Initialize();}
- return sScreen.Boundary.Size.X;
- }
- SHORT ScreenMaxY(void)
- {
- if(!_iOK) {_Initialize();}
- return sScreen.Boundary.Size.Y;
- }
- void GotoXY(const SHORT iX,
- const SHORT iY)
- {
- if(!_iOK) {_Initialize();}
- COORD dwCurPos = {.X = (iX + sWindow.Min.X) - 1,
- .Y = (iY + sWindow.Min.Y) - 1};
- if((dwCurPos.X >= sWindow.Min.X)
- && (dwCurPos.Y >= sWindow.Min.Y)
- && (dwCurPos.X <= sWindow.Max.X)
- && (dwCurPos.Y <= sWindow.Max.Y))
- {
- SetConsoleCursorPosition(hConsoleOutput, dwCurPos);
- }
- }
- void GotoFullXY(const SHORT iX,
- const SHORT iY)
- {
- if(!_iOK) {_Initialize();}
- COORD dwCurPos = {.X = iX - 1,
- .Y = iY - 1};
- if((dwCurPos.X >= sScreen.Boundary.Min.X)
- && (dwCurPos.Y >= sScreen.Boundary.Min.Y)
- && (dwCurPos.X <= sScreen.Boundary.Max.X)
- && (dwCurPos.Y <= sScreen.Boundary.Max.Y))
- {
- SetConsoleCursorPosition(hConsoleOutput, dwCurPos);
- }
- }
- void Window(const SHORT iX1,
- const SHORT iY1,
- const SHORT iX2,
- const SHORT iY2)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX1 - 1,
- .Min.Y = iY1 - 1,
- .Max.X = iX2 - 1,
- .Max.Y = iY2 - 1,
- .Size.X = (iX2 - iX1) + 1,
- .Size.Y = (iY2 - iY1) + 1};
- if((sWind_.Min.X <= sWind_.Max.X)
- && (sWind_.Min.Y <= sWind_.Max.Y)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Max.X <= sScreen.Boundary.Max.X)
- && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
- {
- sWindow = sWind_;
- }
- GotoXY(1,1);
- }
- void WindowFmt(const SHORT iX1,
- const SHORT iY1,
- const SHORT iSizeX,
- const SHORT iSizeY)
- {
- Window(iX1, iY1, iSizeX + iX1 - 1, iSizeY + iY1 - 1);
- }
- void SetColors(const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- sFBColors.wAttributes = wColorFlags;
- SetConsoleTextAttribute(hConsoleOutput, sFBColors.wAttributes);
- }
- void ClrScr(const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
- }
- void ClrFullScr(const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
- }
- void ClrEol(const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = _Where().X,
- .Min.Y = _Where().Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWind_.Min.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, 1, wColorFlags, ucChar);
- }
- }
- void ClrLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = (iY - 1) + sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = (sWind_.Min.Y + iLines) - 1,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Max.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
- }
- }
- void ClrFullLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = iY - 1,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = (sWind_.Min.Y + iLines) - 1,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
- {
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
- }
- }
- void ClrEoc(const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = _Where().X,
- .Min.Y = _Where().Y,
- .Max.X = sWind_.Min.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, CLEAR_LEFT_TO_RIGHT, 1, wColorFlags, ucChar);
- }
- }
- void ClrColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = (iX - 1) + sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = (sWind_.Min.X + iColumns) - 1,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iColumns > 0)
- && (sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Max.X <= sWindow.Max.X))
- {
- _Block(sWind_, CLEAR_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
- }
- }
- void ClrFullColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX - 1,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = (sWind_.Min.X + iColumns) - 1,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iColumns > 0)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Max.X <= sScreen.Boundary.Max.X))
- {
- _Block(sWind_, CLEAR_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
- }
- }
- void InsLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = (iY - 1) + sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = sWindow.Size.X,
- .Size.Y = (sWindow.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (iLines <= sWind_.Size.Y)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, INSERT_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
- }
- }
- void InsFullLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = iY - 1,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = sScreen.Boundary.Size.X,
- .Size.Y = (sScreen.Boundary.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (iLines <= sWind_.Size.Y)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Min.Y <= sScreen.Boundary.Max.Y))
- {
- _Block(sWind_, INSERT_TOP_TO_BOTTOM, iLines, wColorFlags, ucChar);
- }
- }
- void InsColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = (iX - 1) + sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWindow.Max.X - sWind_.Min.X) + 1,
- .Size.Y = sWindow.Size.Y};
- if((iColumns > 0)
- && (iColumns <= sWind_.Size.X)
- && (sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X))
- {
- _Block(sWind_, INSERT_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
- }
- }
- void InsFullColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX - 1,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sScreen.Boundary.Max.X - sWind_.Min.X) + 1,
- .Size.Y = sScreen.Boundary.Size.Y};
- if((iColumns > 0)
- && (iColumns <= sWind_.Size.X)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Min.X <= sScreen.Boundary.Max.X))
- {
- _Block(sWind_, INSERT_LEFT_TO_RIGHT, iColumns, wColorFlags, ucChar);
- }
- }
- void DelLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = (iY - 1) + sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = sWindow.Size.X,
- .Size.Y = (sWindow.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (iLines <= sWind_.Size.Y)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, INSERT_BOTTOM_TO_TOP, iLines, wColorFlags, ucChar);
- }
- }
- void DelFullLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = iY - 1,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = sScreen.Boundary.Size.X,
- .Size.Y = (sScreen.Boundary.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (iLines <= sWind_.Size.Y)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Min.Y <= sScreen.Boundary.Max.Y))
- {
- _Block(sWind_, INSERT_BOTTOM_TO_TOP, iLines, wColorFlags, ucChar);
- }
- }
- void DelColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = (iX - 1) + sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWindow.Max.X - sWind_.Min.X) + 1,
- .Size.Y = sWindow.Size.Y};
- if((iColumns > 0)
- && (iColumns <= sWind_.Size.X)
- && (sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X))
- {
- _Block(sWind_, INSERT_RIGHT_TO_LEFT, iColumns, wColorFlags, ucChar);
- }
- }
- void DelFullColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX - 1,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sScreen.Boundary.Max.X - sWind_.Min.X) + 1,
- .Size.Y = sScreen.Boundary.Size.Y};
- if((iColumns > 0)
- && (iColumns <= sWind_.Size.X)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Min.X <= sScreen.Boundary.Max.X))
- {
- _Block(sWind_, INSERT_RIGHT_TO_LEFT, iColumns, wColorFlags, ucChar);
- }
- }
- void DrawRect(const SHORT iX1,
- const SHORT iY1,
- const SHORT iX2,
- const SHORT iY2,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX1 - 1,
- .Min.Y = iY1 - 1,
- .Max.X = iX2 - 1,
- .Max.Y = iY2 - 1,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((sWind_.Min.X <= sWind_.Max.X)
- && (sWind_.Min.Y <= sWind_.Max.Y)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Max.X <= sScreen.Boundary.Max.X)
- && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
- {
- _Block(sWind_, CLEAR_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ucChar);
- }
- }
- void DrawRectFmt(const SHORT iX1,
- const SHORT iY1,
- const SHORT iSizeX,
- const SHORT iSizeY,
- const DWORD wColorFlags,
- const UCHAR ucChar)
- {
- DrawRect(iX1, iY1, (iX1 + iSizeX) - 1, (iY1 + iSizeY) - 1, wColorFlags, ucChar);
- }
- void RpColor(const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = sWindow.Max.X, .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- _Block(sWind_, REPLACE_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ' ');
- }
- void RpColorFull(const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- _Block(sWind_, REPLACE_TOP_TO_BOTTOM, sWind_.Size.Y, wColorFlags, ' ');
- }
- void RpColorEol(const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = _Where().X,
- .Min.Y = _Where().Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = sWind_.Min.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, REPLACE_TOP_TO_BOTTOM, 1, wColorFlags, ' ');
- }
- }
- void RpColorLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sWindow.Min.X,
- .Min.Y = (iY - 1) + sWindow.Min.Y,
- .Max.X = sWindow.Max.X,
- .Max.Y = (sWind_.Min.Y + iLines) - 1,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Max.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, REPLACE_TOP_TO_BOTTOM, iLines, wColorFlags, ' ');
- }
- }
- void RpColorFullLine(const SHORT iY,
- const SHORT iLines,
- const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = sScreen.Boundary.Min.X,
- .Min.Y = iY - 1,
- .Max.X = sScreen.Boundary.Max.X,
- .Max.Y = (sWind_.Min.Y + iLines) - 1,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iLines > 0)
- && (sWind_.Min.Y >= sScreen.Boundary.Min.Y)
- && (sWind_.Max.Y <= sScreen.Boundary.Max.Y))
- {
- _Block(sWind_, REPLACE_TOP_TO_BOTTOM, iLines, wColorFlags, ' ');
- }
- }
- void RpColorEoc(const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = _Where().X,
- .Min.Y = _Where().Y,
- .Max.X = sWind_.Min.X,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Min.X <= sWindow.Max.X)
- && (sWind_.Min.Y >= sWindow.Min.Y)
- && (sWind_.Min.Y <= sWindow.Max.Y))
- {
- _Block(sWind_, REPLACE_LEFT_TO_RIGHT, 1, wColorFlags, ' ');
- }
- }
- void RpColorColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = (iX - 1) + sWindow.Min.X,
- .Min.Y = sWindow.Min.Y,
- .Max.X = (sWind_.Min.X + iColumns) - 1,
- .Max.Y = sWindow.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iColumns > 0)
- && (sWind_.Min.X >= sWindow.Min.X)
- && (sWind_.Max.X <= sWindow.Max.X))
- {
- _Block(sWind_, REPLACE_LEFT_TO_RIGHT, iColumns, wColorFlags, ' ');
- }
- }
- void RpColorFullColumn(const SHORT iX,
- const SHORT iColumns,
- const DWORD wColorFlags)
- {
- if(!_iOK) {_Initialize();}
- Window_t sWind_ = {.Min.X = iX - 1,
- .Min.Y = sScreen.Boundary.Min.Y,
- .Max.X = (sWind_.Min.X + iColumns) - 1,
- .Max.Y = sScreen.Boundary.Max.Y,
- .Size.X = (sWind_.Max.X - sWind_.Min.X) + 1,
- .Size.Y = (sWind_.Max.Y - sWind_.Min.Y) + 1};
- if((iColumns > 0)
- && (sWind_.Min.X >= sScreen.Boundary.Min.X)
- && (sWind_.Max.X <= sScreen.Boundary.Max.X))
- {
- _Block(sWind_, REPLACE_LEFT_TO_RIGHT, iColumns, wColorFlags, ' ');
- }
- }
- // Intern use.
- void _Initialize(void)
- {
- hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
- GetConsoleScreenBufferInfo(hConsoleOutput, &ConsoleScreenBufferInfo);
- memcpy(&sWindow.Min, &ConsoleScreenBufferInfo.srWindow, sizeof(SMALL_RECT));
- sWindow.Size.X = (sWindow.Max.X - sWindow.Min.X) + 1;
- sWindow.Size.Y = (sWindow.Max.Y - sWindow.Min.Y) + 1;
- memcpy(&sScreen.Window.Min, &sWindow.Min, sizeof(Window_t));
- memcpy(&sScreen.Boundary.Min, &sWindow.Min, sizeof(COORD));
- sScreen.Boundary.Max.X = ConsoleScreenBufferInfo.dwSize.X - 1;
- sScreen.Boundary.Max.Y = ConsoleScreenBufferInfo.dwSize.Y - 1;
- sScreen.Boundary.Size.X = (sScreen.Boundary.Max.X - sScreen.Boundary.Min.X) + 1;
- sScreen.Boundary.Size.Y = (sScreen.Boundary.Max.Y - sScreen.Boundary.Min.Y) + 1;
- _iOK = TRUE;
- }
- COORD _Where(void)
- {
- if(!_iOK) {_Initialize();}
- CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
- GetConsoleScreenBufferInfo(hConsoleOutput, &ConsoleScreenBufferInfo);
- return ConsoleScreenBufferInfo.dwCursorPosition;
- }
- void _Block(const Window_t sWind_,
- const UCHAR cHowInsert,
- const SHORT iHowMany,
- const DWORD wAttributes,
- const UCHAR ucChar)
- {
- if(!_iOK) {_Initialize();}
- SHORT iBlockSize = iHowMany;
- switch(cHowInsert)
- {
- case CLEAR_LEFT_TO_RIGHT :
- case INSERT_RIGHT_TO_LEFT :
- case INSERT_LEFT_TO_RIGHT :
- case REPLACE_LEFT_TO_RIGHT : iBlockSize *= sWind_.Size.Y;
- break;
- case CLEAR_TOP_TO_BOTTOM :
- case INSERT_BOTTOM_TO_TOP :
- case INSERT_TOP_TO_BOTTOM :
- case REPLACE_TOP_TO_BOTTOM : iBlockSize *= sWind_.Size.X;
- break;
- }
- COORD dwBufferSize = {.X = sWind_.Size.X, .Y = sWind_.Size.Y};
- COORD dwBufferCoord = {.X = (cHowInsert == INSERT_LEFT_TO_RIGHT) ? iHowMany : 0,
- .Y = (cHowInsert == INSERT_TOP_TO_BOTTOM) ? iHowMany : 0};
- SMALL_RECT lpReadWriteRegion = {.Left = (cHowInsert == INSERT_RIGHT_TO_LEFT) ? (sWind_.Min.X + iHowMany) : sWind_.Min.X,
- .Top = (cHowInsert == INSERT_BOTTOM_TO_TOP) ? (sWind_.Min.Y + iHowMany) : sWind_.Min.Y,
- .Right = (cHowInsert == INSERT_LEFT_TO_RIGHT) ? (sWind_.Max.X - iHowMany) : sWind_.Max.X,
- .Bottom = (cHowInsert == INSERT_TOP_TO_BOTTOM) ? (sWind_.Max.Y - iHowMany) : sWind_.Max.Y};
- CHAR_INFO (*lpBuffer)[dwBufferSize.X] = (CHAR_INFO(*)[])calloc(dwBufferSize.X * dwBufferSize.Y, sizeof(CHAR_INFO));
- switch(cHowInsert)
- {
- case INSERT_LEFT_TO_RIGHT :
- case INSERT_RIGHT_TO_LEFT :
- case INSERT_TOP_TO_BOTTOM :
- case INSERT_BOTTOM_TO_TOP :
- case REPLACE_LEFT_TO_RIGHT :
- case REPLACE_TOP_TO_BOTTOM : ReadConsoleOutput(hConsoleOutput, (CHAR_INFO*)lpBuffer, dwBufferSize, dwBufferCoord, &lpReadWriteRegion);
- break;
- }
- for(SHORT uiCount = 0; uiCount < iBlockSize; uiCount++)
- {
- SHORT iLine = 0, iColumn = 0;
- switch(cHowInsert)
- {
- case INSERT_RIGHT_TO_LEFT : iColumn = (sWind_.Size.X - iHowMany);
- case CLEAR_LEFT_TO_RIGHT :
- case INSERT_LEFT_TO_RIGHT :
- case REPLACE_LEFT_TO_RIGHT : iLine = (uiCount % sWind_.Size.Y);
- iColumn += (uiCount / sWind_.Size.Y);
- break;
- case INSERT_BOTTOM_TO_TOP : iLine = (sWind_.Size.Y - iHowMany);
- case CLEAR_TOP_TO_BOTTOM :
- case INSERT_TOP_TO_BOTTOM :
- case REPLACE_TOP_TO_BOTTOM : iLine += (uiCount / sWind_.Size.X);
- iColumn = (uiCount % sWind_.Size.X);
- break;
- }
- switch(cHowInsert)
- {
- default : lpBuffer[iLine][iColumn].Char.AsciiChar = ucChar;
- case REPLACE_LEFT_TO_RIGHT :
- case REPLACE_TOP_TO_BOTTOM : lpBuffer[iLine][iColumn].Attributes = wAttributes;
- break;
- }
- }
- switch(cHowInsert)
- {
- case INSERT_LEFT_TO_RIGHT : dwBufferCoord.X -= iHowMany;
- lpReadWriteRegion.Right += iHowMany;
- break;
- case INSERT_RIGHT_TO_LEFT : lpReadWriteRegion.Left -= iHowMany;
- break;
- case INSERT_TOP_TO_BOTTOM : dwBufferCoord.Y -= iHowMany;
- lpReadWriteRegion.Bottom += iHowMany;
- break;
- case INSERT_BOTTOM_TO_TOP : lpReadWriteRegion.Top -= iHowMany;
- break;
- }
- WriteConsoleOutput(hConsoleOutput, (CHAR_INFO*)lpBuffer, dwBufferSize, dwBufferCoord, &lpReadWriteRegion);
- free(lpBuffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement