sapitando

WinAPI - Resize Console Window (Free Pascal)

Aug 10th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.42 KB | None | 0 0
  1. { Autor : Tiago Portela
  2.   Email : sapitando@gmail.com
  3.   Sobre o programa : Muda o tamanho da janela do console do Windows, a largura(variável Cols) tem que ser maior ou igual a 14,
  4.                      fiz o teste apenas no windows 7.
  5.   Obs. : Apenas tentando aprender algorítimos, sozinho, por hobby. }
  6.  
  7. program Resize_Console_Window;
  8. uses JwaWindows, Crt;
  9.  
  10. var
  11.   Cols, Lines : SHORT;
  12.   SysMenuButtons : boolean;
  13.   TitleWindow : PChar;
  14.   WindowError : BOOL;
  15.  
  16.  
  17. function SetConsoleWindowInfoAndBufferSize(X, Y : SHORT;
  18.            B : boolean; TW : PChar) : BOOL;
  19.  
  20. var
  21.   hConsoleOutput : HANDLE;
  22.   dwSize : COORD;
  23.   lpCW : SMALL_RECT = (Left : 0; Top : 0);
  24.  
  25. begin
  26.   if (not (MoveWindow(GetConsoleWindow, 0, 0, 1, 1, TRUE)))
  27.   then begin
  28.          INT(SetConsoleWindowInfoAndBufferSize) := 1;
  29.          exit;
  30.        end;
  31.   if (not (SetConsoleTitle(TW)))
  32.   then begin
  33.          INT(SetConsoleWindowInfoAndBufferSize) := 2;
  34.          exit;
  35.        end;
  36.   if (not (B))
  37.   then begin
  38.          if (SetWindowLong(GetConsoleWindow, GWL_STYLE,
  39.            GetWindowLong(GetConsoleWindow, GWL_STYLE) and
  40.            (not (WS_SYSMENU or WS_HSCROLL or WS_VSCROLL))) = 0)
  41.          then begin
  42.                 INT(SetConsoleWindowInfoAndBufferSize) := 3;
  43.                 exit;
  44.               end;
  45.        end;
  46.   hConsoleOutput := GetStdHandle(STD_OUTPUT_HANDLE);
  47.   if (hConsoleOutput = INVALID_HANDLE_VALUE)
  48.   then begin
  49.          INT(SetConsoleWindowInfoAndBufferSize) := 4;
  50.          exit;
  51.        end;
  52.   dwSize := GetLargestConsoleWindowSize(hConsoleOutput);
  53.   if (X > dwSize.X)
  54.   then X := dwSize.X;
  55.   if (Y > dwSize.Y)
  56.   then Y := dwSize.Y;
  57.   dwSize.X := X;
  58.   dwSize.Y := Y;
  59.   if (not (SetConsoleScreenBufferSize(hConsoleOutput, dwSize)))
  60.   then begin
  61.          INT(SetConsoleWindowInfoAndBufferSize) := 5;
  62.          exit;
  63.        end;
  64.   lpCW.Right := X - 1;
  65.   lpCW.Bottom := Y - 1;
  66.   if (not (SetConsoleWindowInfo(hConsoleOutput, TRUE, lpCW)))
  67.   then begin
  68.          INT(SetConsoleWindowInfoAndBufferSize) := 6;
  69.          exit;
  70.        end;
  71.   window(1, 1, X, Y);
  72.   INT(SetConsoleWindowInfoAndBufferSize) := 0;
  73. end;
  74.  
  75.  
  76. begin
  77.   Cols := 40;
  78.   Lines := 20;
  79.   SysMenuButtons := FALSE;
  80.   TitleWindow := 'My Window';
  81.   WindowError := SetConsoleWindowInfoAndBufferSize(Cols, Lines,
  82.                    SysMenuButtons, TitleWindow);
  83.   if WindowError
  84.   then writeln(INT(WindowError));
  85.   textbackground(red);
  86.   clrscr;
  87.   readln;
  88. end.
Add Comment
Please, Sign In to add comment