Advertisement
sapitando

Unit consoleWindows

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