Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { Autor : Tiago Portela
- Email : sapitando@gmail.com
- Obs. : Apenas tentando aprender algorítimos, sozinho, por hobby. }
- unit consoleWindows;
- {$MODE DELPHI}
- interface
- uses jwawincon, jwawintype, Windows, variants, crt;
- const
- _BLACK = 0;
- _BLUE = 1;
- _GREEN = 2;
- _CYAN = 3;
- _RED = 4;
- _MAGENT = 5;
- _YELLOW = 6;
- _WHITE = 7;
- _INTENSITY = 8;
- _BRIGHTBLUE = 9;
- _BRIGHTGREEN = 10;
- _BRIGHTCYAN = 11;
- _BRIGHTRED = 12;
- _BRIGHTMAGENT = 13;
- _BRIGHTYELLOW = 14;
- _BRIGHTWHITE = 15;
- _Co80 = 1;
- _Co40 = 2;
- _C80 = Co80;
- _C40 = Co40;
- _C80L25 = Co80;
- _C80L50 = 3;
- _CMAX = 4;
- _CMAXB1000 = 5;
- procedure _InitConsole;
- procedure _TextMode(Mode : byte);
- procedure _HideConsole;
- procedure _ShowConsole;
- function _WhereX : SHORT;
- function _WhereY : SHORT;
- procedure _GotoXY(const X : SHORT;const Y : SHORT);
- procedure _Window(const X1 : WORD; const Y1 : WORD; const X2 : WORD; const Y2 : WORD);
- procedure _ClrEol;
- procedure _ClrScr;
- procedure _TextColor(const AttrForeground : WORD);
- procedure _TextBackground(const AttrBackground : WORD);
- procedure _Write(const Text_ : variant);
- procedure _WriteLn(const Text_ : variant);
- implementation
- const
- MAXSIZE = 0;
- var
- hWndConsole : HWND;
- hConsoleOutput : HANDLE;
- CSBI : CONSOLE_SCREEN_BUFFER_INFO;
- Attr : WORD;
- WindMinX : WORD;
- WindMinY : WORD;
- WindMaxX : WORD;
- WindMaxY : WORD;
- procedure _InitConsole;
- begin
- if (hWndConsole = 0)
- then begin
- AllocConsole;
- hWndConsole := GetConsoleWindow;
- hConsoleOutput := GetStdHandle(STD_OUTPUT_HANDLE);
- SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOSIZE);
- GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
- WindMaxX := CSBI.dwSize.X;
- WindMaxY := CSBI.dwSize.Y;
- _GotoXY(1, 1);
- SetWindowLong(hWndConsole, GWL_STYLE, GetWindowLong(hWndConsole, GWL_STYLE) and (not(WS_HSCROLL or WS_THICKFRAME)));
- // SetWindowLong(hWndConsole, GWL_STYLE, GetWindowLong(hWndConsole, GWL_STYLE) and (not(WS_SYSMENU or WS_HSCROLL or WS_THICKFRAME)));
- end;
- end;
- procedure ResizeConsole(const Arg : array of WORD);
- var
- dwSize : COORD;
- srWindow : SMALL_RECT = (Left : 0; Top : 0);
- begin
- if (high(Arg) in [1,2]) and (Arg[0] <> MAXSIZE) and (Arg[1] <> MAXSIZE)
- then begin
- dwSize.X := Arg[0];
- srWindow.Right := Arg[0] - 1;
- srWindow.Bottom := Arg[1] - 1;
- dwSize.X := Arg[0];
- if (high(Arg) = 2)
- then dwSize.Y := Arg[2]
- else dwSiZe.Y := Arg[1];
- end
- else begin
- dwSize := GetLargestConsoleWindowSize(hConsoleOutput);
- dec(dwSize.X, 2);
- dec(dwSize.Y, 1);
- srWindow.Right := dwSize.X - 1;
- srWindow.Bottom := dwSiZe.Y - 1;
- if (high(Arg) = 2)
- then dwSize.Y := Arg[2];
- end;
- WindMaxX := srWindow.Right;
- WindMaxY := srWindow.Bottom;
- SetWindowPos(hWndConsole, 0, 0, 0, 1, 1, SWP_NOMOVE);
- SetConsoleScreenBufferSize(hConsoleOutput, dwSize);
- SetConsoleWindowInfo(hConsoleOutput, TRUE, srWindow);
- end;
- procedure _TextMode(Mode : byte);
- begin
- case Mode of
- 1 : ResizeConsole([80, 25]); // ResizeConsole([80, 25, 25])
- 2 : ResizeConsole([40, 25]);
- 3 : ResizeConsole([80, 50, 50]);
- 4 : ResizeConsole([]);
- 5 : ResizeConsole([MAXSIZE, MAXSIZE, 1000]);
- end;
- end;
- procedure _HideConsole;
- begin
- ShowWindow(hWndConsole, SW_HIDE);
- end;
- procedure _ShowConsole;
- begin
- ShowWindow(hWndConsole, SW_RESTORE);
- end;
- function _WhereX : SHORT;
- begin
- GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
- result := CSBI.dwCursorPosition.X;
- end;
- function _WhereY : SHORT;
- begin
- GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
- result := CSBI.dwCursorPosition.Y;
- end;
- procedure _GotoXY(const X : SHORT; const Y : SHORT);
- var
- dwSize : COORD;
- begin
- if (X <= WindMaxX) and (Y <= WindMaxY) and (X >= WindMinX) and (Y >= WindMinY)
- then begin
- dwSize.X := X;
- dwSize.Y := Y;
- SetConsoleCursorPosition(hConsoleOutput, dwSize);
- end;
- end;
- procedure _Window(const X1 : WORD; const Y1 : WORD; const X2 : WORD; const Y2 : WORD);
- begin
- WindMinX := X1;
- WindMinY := Y1;
- WindMaxX := X2;
- WindMaxY := Y2;
- _GotoXY(X1, Y1);
- end;
- procedure _ClrEol;
- var
- lpNumberOfCharsWritten : DWORD;
- begin
- GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
- FillConsoleOutputAttribute(hConsoleOutput, Attr, (WindMaxX - CSBI.dwCursorPosition.X + 1), CSBI.dwCursorPosition, lpNumberOfCharsWritten);
- FillConsoleOutputCharacter(hConsoleOutput, ' ', (WindMaxX - CSBI.dwCursorPosition.X + 1), CSBI.dwCursorPosition, lpNumberOfCharsWritten);
- end;
- procedure _ClrScr;
- var
- dwWriteCoord : COORD;
- lpNumberOfCharsWritten : DWORD;
- begin
- dwWriteCoord.X := WindMinX;
- dwWriteCoord.Y := WindMinY;
- while ((dwWriteCoord.Y <= WindMaxY)) do
- begin
- FillConsoleOutputAttribute(hConsoleOutput, Attr, (WindMaxX - WindMinX + 1), dwWriteCoord, lpNumberOfCharsWritten);
- FillConsoleOutputCharacter(hConsoleOutput, ' ', (WindMaxX - WindMinX + 1), dwWriteCoord, lpNumberOfCharsWritten);
- inc(dwWriteCoord.Y);
- end;
- end;
- procedure _TextColor(const AttrForeground : WORD);
- begin
- Attr := (byte(Attr shr 4) shl 4) or AttrForeground;
- SetConsoleTextAttribute(hConsoleOutput, Attr);
- end;
- procedure _TextBackground(const AttrBackground : WORD);
- begin
- Attr := (byte(Attr shl 4) shr 4) or (AttrBackground shl 4);
- SetConsoleTextAttribute(hConsoleOutput, Attr);
- end;
- procedure _Write(const Text_ : variant);
- var
- lpBuffer : string;
- lpNumberOfCharsWritten : DWORD;
- wSizeText : WORD;
- wSizeLine : WORD;
- wPosText : WORD = 0;
- begin
- lpBuffer := Text_;
- wSizeText := length(lpBuffer);
- While (wPosText < wSizeText) do
- begin
- if ((wSizeText - wPosText) > (WindMaxX - _WhereX))
- then wSizeLine := WindMaxX - _WhereX + 1
- else wSizeLine := wSizeText - wPosText;
- WriteConsole(hConsoleOutput, @lpBuffer[wPosText + 1], wSizeLine, lpNumberOfCharsWritten, 0);
- wPosText := wPosText + wSizeLine;
- if (wPosText < wSizeText) or (_WhereX > WindMaxX)
- then _GotoXY(WindMinX, _WhereY + 1);
- end;
- end;
- procedure _WriteLn(const Text_ : variant);
- begin
- _Write(Text_);
- _GotoXY(WindMinX, _WhereY + 1);
- end;
- initialization
- begin
- hWndConsole := GetConsoleWindow;
- if (hWndConsole <> 0)
- then begin
- hConsoleOutput := GetStdHandle(STD_OUTPUT_HANDLE);
- GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
- SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOSIZE);
- WindMinX := CSBI.srWindow.Left;
- WindMinY := CSBI.srWindow.Top;
- WindMaxX := CSBI.srWindow.Right;
- WindMaxY := CSBI.srWindow.Bottom;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement