Changing the console size

2019-05-28 01:38发布

问题:

Simple problem in Delphi. I've created a console application and I need to change the height of the console window to 80 lines, if it's less than 80 lines. This need to be done from code and is actually conditional within the code. (I.e. when an error occurs, it increases the size of the console so the whole (huge) error report is visible.)
Keep in mind that this is a console application! When it starts, it uses the default console, which I need to alter!

回答1:

When calling SetConsoleWindowInfo() the values for Left and Top that are passed to the console need to at least be 1, not 0. Problem solved.

I now do this:

uses
  Windows;

var
  Rect: TSmallRect;
  Coord: TCoord;
begin
  Rect.Left := 1;
  Rect.Top := 1;
  Rect.Right := 80;
  Rect.Bottom := 60;
  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;


回答2:

procedure SetConsoleWindow(NewWidth : integer;NewHeight : integer);

var
  Rect: TSmallRect;
  Coord: TCoord;
  begin { SetConsoleWindow }
  Coord.X := NewWidth;
  Coord.y := NewHeight;
  SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), Coord);
  Rect.Left := 0;   //  must be zero
  Rect.Top := 0;
  Rect.Right := Coord.X - (Rect.Left + 1);
  Rect.Bottom := Coord.y - (Rect.Top + 1);
  SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), True, Rect);
  end; { SetConsoleWindow }