Advertisement
sapitando

Unit consoleWindows

Aug 20th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.98 KB | None | 0 0
  1. { Autor : Tiago Portela
  2.   Email : sapitando@gmail.com
  3.   Obs. : Apenas tentando aprender algorítimos, sozinho, por hobby. }
  4.  
  5. unit consoleWindows;
  6.  
  7. {$MODE DELPHI}
  8.  
  9. interface
  10. uses jwawincon, jwawintype, Windows, variants, crt;
  11.  
  12. const
  13.  _BLACK        = 0;
  14.  _BLUE         = 1;
  15.  _GREEN        = 2;
  16.  _CYAN         = 3;
  17.  _RED          = 4;
  18.  _MAGENT       = 5;
  19.  _YELLOW       = 6;
  20.  _WHITE        = 7;
  21.  _INTENSITY    = 8;
  22.  _BRIGHTBLUE   = 9;
  23.  _BRIGHTGREEN  = 10;
  24.  _BRIGHTCYAN   = 11;
  25.  _BRIGHTRED    = 12;
  26.  _BRIGHTMAGENT = 13;
  27.  _BRIGHTYELLOW = 14;
  28.  _BRIGHTWHITE  = 15;
  29.  _Co80         = 1;
  30.  _Co40         = 2;
  31.  _C80          = Co80;
  32.  _C40          = Co40;
  33.  _C80L25       = Co80;
  34.  _C80L50       = 3;
  35.  _CMAX         = 4;
  36.  _CMAXB1000    = 5;
  37.  
  38. procedure _InitConsole;
  39. procedure _TextMode(Mode : byte);
  40. procedure _HideConsole;
  41. procedure _ShowConsole;
  42. function _WhereX : SHORT;
  43. function _WhereY : SHORT;
  44. procedure _GotoXY(const X : SHORT;const Y : SHORT);
  45. procedure _Window(const X1 : WORD; const Y1 : WORD; const X2 : WORD; const Y2 : WORD);
  46. procedure _ClrEol;
  47. procedure _ClrScr;
  48. procedure _TextColor(const AttrForeground : WORD);
  49. procedure _TextBackground(const AttrBackground : WORD);
  50. procedure _Write(const Text_ : variant);
  51. procedure _WriteLn(const Text_ : variant);
  52.  
  53. implementation
  54. const
  55.   MAXSIZE = 0;
  56.  
  57. var
  58.   hWndConsole    : HWND;
  59.   hConsoleOutput : HANDLE;
  60.   CSBI           : CONSOLE_SCREEN_BUFFER_INFO;
  61.   Attr           : WORD;
  62.   WindMinX       : WORD;
  63.   WindMinY       : WORD;
  64.   WindMaxX       : WORD;
  65.   WindMaxY       : WORD;
  66.  
  67. procedure _InitConsole;
  68. begin
  69.   if (hWndConsole = 0)
  70.   then begin
  71.          AllocConsole;
  72.          hWndConsole := GetConsoleWindow;
  73.          hConsoleOutput := GetStdHandle(STD_OUTPUT_HANDLE);
  74.          SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOSIZE);
  75.          GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
  76.          WindMaxX := CSBI.dwSize.X;
  77.          WindMaxY := CSBI.dwSize.Y;
  78.          _GotoXY(1, 1);
  79.          SetWindowLong(hWndConsole, GWL_STYLE, GetWindowLong(hWndConsole, GWL_STYLE) and (not(WS_HSCROLL or WS_THICKFRAME)));
  80. //       SetWindowLong(hWndConsole, GWL_STYLE, GetWindowLong(hWndConsole, GWL_STYLE) and (not(WS_SYSMENU or WS_HSCROLL or WS_THICKFRAME)));
  81.        end;
  82. end;
  83.  
  84. procedure ResizeConsole(const Arg : array of WORD);
  85. var
  86.   dwSize        : COORD;
  87.   srWindow      : SMALL_RECT = (Left : 0; Top : 0);
  88. begin
  89.   if (high(Arg) in [1,2]) and (Arg[0] <> MAXSIZE) and (Arg[1] <> MAXSIZE)
  90.   then begin
  91.          dwSize.X := Arg[0];
  92.          srWindow.Right := Arg[0] - 1;
  93.          srWindow.Bottom := Arg[1] - 1;
  94.          dwSize.X := Arg[0];
  95.          if (high(Arg) = 2)
  96.          then dwSize.Y := Arg[2]
  97.          else dwSiZe.Y := Arg[1];
  98.        end
  99.   else begin
  100.          dwSize := GetLargestConsoleWindowSize(hConsoleOutput);
  101.          dec(dwSize.X, 2);
  102.          dec(dwSize.Y, 1);
  103.          srWindow.Right := dwSize.X - 1;
  104.          srWindow.Bottom := dwSiZe.Y - 1;
  105.          if (high(Arg) = 2)
  106.          then dwSize.Y := Arg[2];
  107.        end;
  108.   WindMaxX := srWindow.Right;
  109.   WindMaxY := srWindow.Bottom;
  110.   SetWindowPos(hWndConsole, 0, 0, 0, 1, 1, SWP_NOMOVE);
  111.   SetConsoleScreenBufferSize(hConsoleOutput, dwSize);
  112.   SetConsoleWindowInfo(hConsoleOutput, TRUE, srWindow);
  113. end;
  114.  
  115. procedure _TextMode(Mode : byte);
  116. begin
  117.   case Mode of
  118.     1 : ResizeConsole([80, 25]);      // ResizeConsole([80, 25, 25])
  119.     2 : ResizeConsole([40, 25]);    
  120.     3 : ResizeConsole([80, 50, 50]);  
  121.     4 : ResizeConsole([]);
  122.     5 : ResizeConsole([MAXSIZE,  MAXSIZE,  1000]);
  123.   end;
  124. end;
  125.  
  126. procedure _HideConsole;
  127. begin
  128.   ShowWindow(hWndConsole, SW_HIDE);
  129. end;
  130.  
  131. procedure _ShowConsole;
  132. begin
  133.   ShowWindow(hWndConsole, SW_RESTORE);
  134. end;
  135.  
  136. function _WhereX : SHORT;
  137. begin
  138.   GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
  139.   result := CSBI.dwCursorPosition.X;
  140. end;
  141.  
  142. function _WhereY : SHORT;
  143. begin
  144.   GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
  145.   result := CSBI.dwCursorPosition.Y;
  146. end;
  147.  
  148. procedure _GotoXY(const X : SHORT; const Y : SHORT);
  149. var
  150.   dwSize : COORD;
  151. begin
  152.     if (X <= WindMaxX) and (Y <= WindMaxY) and (X >= WindMinX) and (Y >= WindMinY)
  153.     then begin
  154.            dwSize.X := X;
  155.            dwSize.Y := Y;
  156.            SetConsoleCursorPosition(hConsoleOutput, dwSize);
  157.          end;
  158. end;
  159.  
  160. procedure _Window(const X1 : WORD; const Y1 : WORD; const X2 : WORD; const Y2 : WORD);
  161. begin
  162.   WindMinX := X1;
  163.   WindMinY := Y1;
  164.   WindMaxX := X2;
  165.   WindMaxY := Y2;
  166.   _GotoXY(X1, Y1);
  167. end;
  168.  
  169. procedure _ClrEol;
  170. var
  171.   lpNumberOfCharsWritten : DWORD;
  172. begin
  173.   GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
  174.   FillConsoleOutputAttribute(hConsoleOutput, Attr, (WindMaxX - CSBI.dwCursorPosition.X + 1), CSBI.dwCursorPosition, lpNumberOfCharsWritten);
  175.   FillConsoleOutputCharacter(hConsoleOutput, ' ', (WindMaxX - CSBI.dwCursorPosition.X + 1), CSBI.dwCursorPosition, lpNumberOfCharsWritten);
  176. end;
  177.  
  178. procedure _ClrScr;
  179. var
  180.   dwWriteCoord : COORD;
  181.   lpNumberOfCharsWritten : DWORD;
  182. begin
  183.   dwWriteCoord.X := WindMinX;
  184.   dwWriteCoord.Y := WindMinY;
  185.   while ((dwWriteCoord.Y <= WindMaxY)) do
  186.     begin
  187.       FillConsoleOutputAttribute(hConsoleOutput, Attr, (WindMaxX - WindMinX + 1), dwWriteCoord, lpNumberOfCharsWritten);
  188.       FillConsoleOutputCharacter(hConsoleOutput, ' ', (WindMaxX - WindMinX + 1), dwWriteCoord, lpNumberOfCharsWritten);
  189.       inc(dwWriteCoord.Y);
  190.     end;
  191. end;
  192.  
  193. procedure _TextColor(const AttrForeground : WORD);
  194. begin
  195.   Attr := (byte(Attr shr 4) shl 4) or AttrForeground;
  196.   SetConsoleTextAttribute(hConsoleOutput, Attr);
  197. end;
  198.  
  199. procedure _TextBackground(const AttrBackground : WORD);
  200. begin
  201.   Attr := (byte(Attr shl 4) shr 4) or (AttrBackground shl 4);
  202.   SetConsoleTextAttribute(hConsoleOutput, Attr);
  203. end;
  204.  
  205. procedure _Write(const Text_ : variant);
  206. var
  207.   lpBuffer               : string;
  208.   lpNumberOfCharsWritten : DWORD;
  209.   wSizeText              : WORD;
  210.   wSizeLine              : WORD;
  211.   wPosText               : WORD = 0;
  212. begin
  213.   lpBuffer := Text_;
  214.   wSizeText := length(lpBuffer);
  215.   While (wPosText < wSizeText) do
  216.     begin
  217.       if ((wSizeText - wPosText) > (WindMaxX - _WhereX))
  218.       then wSizeLine := WindMaxX - _WhereX + 1
  219.       else wSizeLine := wSizeText - wPosText;
  220.       WriteConsole(hConsoleOutput, @lpBuffer[wPosText + 1], wSizeLine, lpNumberOfCharsWritten, 0);
  221.       wPosText := wPosText + wSizeLine;
  222.       if (wPosText < wSizeText) or (_WhereX > WindMaxX)
  223.       then _GotoXY(WindMinX, _WhereY + 1);
  224.     end;
  225. end;
  226.  
  227.  
  228. procedure _WriteLn(const Text_ : variant);
  229. begin
  230.   _Write(Text_);
  231.   _GotoXY(WindMinX, _WhereY + 1);
  232. end;
  233.  
  234. initialization
  235. begin
  236.   hWndConsole := GetConsoleWindow;
  237.   if (hWndConsole <> 0)
  238.   then begin
  239.          hConsoleOutput := GetStdHandle(STD_OUTPUT_HANDLE);
  240.          GetConsoleScreenBufferInfo(hConsoleOutput, CSBI);
  241.          SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOSIZE);
  242.          WindMinX := CSBI.srWindow.Left;
  243.          WindMinY := CSBI.srWindow.Top;
  244.          WindMaxX := CSBI.srWindow.Right;
  245.          WindMaxY := CSBI.srWindow.Bottom;
  246.        end;
  247. end;
  248.  
  249. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement