Reputation: 63
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!
Upvotes: 2
Views: 2147
Reputation: 54812
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.
Upvotes: 4
Reputation: 30715
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.
Upvotes: 2