windowsconsolelazarusfreepascal

How can I increase the size of the console inside the code


I'm doing some code in pascal using lazarus IDE v1.8.4, as the question says I need to be able to edit the console size in the code, I also preferably need to get the max possible console width they can have. If you do know how please also let me know the uses you.. used. Thanks!


Solution

  • Assuming you're targeting Windows:

    At this point the console's window should be positioned as you've set. With my tests, however, while the window complies with the sizing request, the position is ignored.

    In that case use any API function to move the window, the below examples uses SetWindowPos. I had to declare GetConsoleWindow as it was not declared in Lazarus 1.6.


    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      windows;
    
    function GetConsoleWindow: HWND; stdcall external 'kernel32';
    
    var
      Con: THandle;
      Size: TCoord;
      Rect: TSmallRect;
      Wnd: HWND;
    begin
      Con := GetStdHandle(STD_OUTPUT_HANDLE);
      Size := GetLargestConsoleWindowSize(Con);
    
      SetConsoleScreenBufferSize(Con, Size);
    
      Rect.Left := -10;
      Rect.Top := -10;
      Rect.Right := Size.X - 11;
      Rect.Bottom := Size.Y - 11;
      SetConsoleWindowInfo(Con, True, Rect);
    
      Wnd := GetConsoleWindow;
      SetWindowPos(Wnd, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
    
      Readln;
    end.
    


    And don't forget to add error checking.