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

橙三吉。 提交于 2019-12-12 01:25:34

问题


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!


回答1:


Assuming you're targeting Windows:

  • Use GetLargestConsoleWindowSize to retrieve the largest possible console size depending on the console font and display settings,

  • Use SetConsoleScreenBufferSize to set the console screen buffer to the largest possible size,

  • Use SetConsoleWindowInfo to set the size and position of the console's window, so that no scrollbars would be visible by default etc..

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.




回答2:


This seems to work fine in Lazarus for me on Win10Pro.

program ResizeConsoleWin;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows;

procedure SetConsoleWindowSize;
var
  Rect: TSmallRect;
  Coord: TCoord;
begin
  Rect.Left := 1;
  Rect.Top := 1;
  Rect.Right := 300;  // notice horiz scroll bar once the following executes
  Rect.Bottom := 30;
  Coord.X := Rect.Right + 1 - Rect.Left;
  Coord.y := Rect.Bottom + 1 - Rect.Top;
  SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), Coord);
  SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), True, Rect);
end;

begin
  SetConsoleWindowSize;
  readln;
end.

It's copied from this answer with only the window dimensions changed.



来源:https://stackoverflow.com/questions/53769244/how-can-i-increase-the-size-of-the-console-inside-the-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!