Page 1 of 1

Issue with Resizing the Console Window

Posted: Sun Oct 20, 2024 9:02 am
by Armoured
Hi,
The purpose of this procedure is to resize the console window and its buffer.
Unfortunately, I keep getting error 87 ("The parameter is incorrect.").
What am I doing wrong? :?

Thanks

Code: Select all

Procedure ResizeConsole(Cx, Cy)
  Protected stdout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  
  ; Set the buffer size
  If (SetConsoleScreenBufferSize_(stdout, Cx | (Cy << 16)) = 0)
    Debug "Error setting buffer size: " + Str(GetLastError_())
    ProcedureReturn ;Exit the procedure in case of an error.
  EndIf
  
  ; Set the window size
  Protected rect.SMALL_RECT
  rect\left = 0
  rect\top = 0
  If (Cx > 0) : rect\right = Cx - 1 : EndIf
  If (Cy > 0) : rect\bottom = Cy - 1 : EndIf
  
   ; Resize the console window
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
 
EndProcedure

OpenConsole()
ResizeConsole(50,40) ;Error 87 "The parameter is incorrect."
Input()
ResizeConsole(70,40) 
Input()

Re: Issue with Resizing the Console Window

Posted: Sun Oct 20, 2024 1:08 pm
by ChrisR
SetConsoleScreenBufferSize: The specified width and height cannot be less than the width and height of the console screen buffer's window.
To get around, you can shrink the window to a minimum before changing the buffer size

Code: Select all

Procedure ResizeConsole(Cx, Cy)
  If Cx <= 0 Or Cy <= 0 : ProcedureReturn : EndIf
  
  Protected stdout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  ; Set the window size
  Protected rect.SMALL_RECT
  rect\right = 1
  rect\bottom = 1
  ; Resize the console window to minimal size
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
  ; Set the buffer size
  If (SetConsoleScreenBufferSize_(stdout, Cx | (Cy << 16)) = 0)
    Debug "Error setting buffer size: " + Str(GetLastError_())
    ProcedureReturn ;Exit the procedure in case of an error.
  EndIf
  
  ; Set the window size
  rect\right = Cx - 1
  rect\bottom = Cy - 1
  ; Resize the console window
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
EndProcedure

OpenConsole()
ResizeConsole(50,20)
Input()
ResizeConsole(70,20) 
Input()

Re: Issue with Resizing the Console Window

Posted: Sun Oct 20, 2024 3:02 pm
by Armoured
ChrisR wrote: Sun Oct 20, 2024 1:08 pm SetConsoleScreenBufferSize: The specified width and height cannot be less than the width and height of the console screen buffer's window.
To get around, you can shrink the window to a minimum before changing the buffer size

Code: Select all

Procedure ResizeConsole(Cx, Cy)
  If Cx <= 0 Or Cy <= 0 : ProcedureReturn : EndIf
  
  Protected stdout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  ; Set the window size
  Protected rect.SMALL_RECT
  rect\right = 1
  rect\bottom = 1
  ; Resize the console window to minimal size
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
  ; Set the buffer size
  If (SetConsoleScreenBufferSize_(stdout, Cx | (Cy << 16)) = 0)
    Debug "Error setting buffer size: " + Str(GetLastError_())
    ProcedureReturn ;Exit the procedure in case of an error.
  EndIf
  
  ; Set the window size
  rect\right = Cx - 1
  rect\bottom = Cy - 1
  ; Resize the console window
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
EndProcedure

OpenConsole()
ResizeConsole(50,20)
Input()
ResizeConsole(70,20) 
Input()
Hi ChrisR,
Your example works perfectly with the two values above, but if we specify ResizeConsole(77,99), the usual error 87 reappears.
Why?

Re: Issue with Resizing the Console Window

Posted: Sun Oct 20, 2024 3:41 pm
by Armoured
Armoured wrote: Sun Oct 20, 2024 3:02 pm
ChrisR wrote: Sun Oct 20, 2024 1:08 pm SetConsoleScreenBufferSize: The specified width and height cannot be less than the width and height of the console screen buffer's window.
To get around, you can shrink the window to a minimum before changing the buffer size

Code: Select all

Procedure ResizeConsole(Cx, Cy)
  If Cx <= 0 Or Cy <= 0 : ProcedureReturn : EndIf
  
  Protected stdout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  ; Set the window size
  Protected rect.SMALL_RECT
  rect\right = 1
  rect\bottom = 1
  ; Resize the console window to minimal size
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
  ; Set the buffer size
  If (SetConsoleScreenBufferSize_(stdout, Cx | (Cy << 16)) = 0)
    Debug "Error setting buffer size: " + Str(GetLastError_())
    ProcedureReturn ;Exit the procedure in case of an error.
  EndIf
  
  ; Set the window size
  rect\right = Cx - 1
  rect\bottom = Cy - 1
  ; Resize the console window
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
EndProcedure

OpenConsole()
ResizeConsole(50,20)
Input()
ResizeConsole(70,20) 
Input()
Hi ChrisR,
Your example works perfectly with the two values above, but if we specify ResizeConsole(77,99), the usual error 87 reappears.
Why?
I think I’ve found the problem this time: it’s caused by SetConsoleWindowInfo setting a value that’s too large for the window height, which leads to the error. The solution is to use GetConsoleScreenBufferInfo to ensure that these values are never exceeded. Thanks, ChrisR, both for the help you’ve given me and for your GUI editor, which has been useful to me on many occasions.

Re: Issue with Resizing the Console Window

Posted: Sun Oct 20, 2024 3:47 pm
by ChrisR
Yes for the ConsoleBufferInfo MaximumWindowSize
But I don't know how to do for the minimum size. 16x1 looks good here.

Code: Select all

Procedure Min(ValueA, ValueB)
  If ValueA < ValueB
    ProcedureReturn ValueA
  EndIf
  ProcedureReturn ValueB
EndProcedure

Procedure ResizeConsole(Cx, Cy)
  If Cx < 16 Or Cy < 1 : ProcedureReturn : EndIf
  Protected ConsoleBufferInfo.CONSOLE_SCREEN_BUFFER_INFO
  Protected stdout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  ; Set the window size
  Protected rect.SMALL_RECT
  ; Resize the console window to minimal size
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  ;GetConsoleScreenBufferInfo_(stdout, @ConsoleBufferInfo)
  ; Set the buffer size
  If (SetConsoleScreenBufferSize_(stdout, Cx | (Cy << 16)) = 0)
    Debug "Error setting buffer size: " + Str(GetLastError_())
    ProcedureReturn ;Exit the procedure in case of an error.
  EndIf
  GetConsoleScreenBufferInfo_(stdout, @ConsoleBufferInfo)
  ; Set the window size
  rect\right  = Min(Cx - 1, ConsoleBufferInfo\dwMaximumWindowSize\x - 1)
  rect\bottom = Min(Cy - 1, ConsoleBufferInfo\dwMaximumWindowSize\y - 1)
  ; Resize the console window
  If (SetConsoleWindowInfo_(stdout, 1, @rect) = 0)
    Debug "Error resizing console window: " + Str(GetLastError_())
  EndIf
  
EndProcedure

OpenConsole()
ResizeConsole(16,1) ;Error 87 "The parameter is incorrect."
Input()
ResizeConsole(77,99) 
Input()