Resizing the Console Window (with code)

Share your advanced PureBasic knowledge/code with the community.
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Resizing the Console Window (with code)

Post by oldefoxx »

Code updated for 5.20+

I don't know whether to put the outcome down as a feature request, or bug report, so I am just going to post the program and let you decide for yourselves.

The concept of having a Console compatability is primarily to serve those who need a simple screen process - a part of the concept of migrating DOS programmers and programs into the world of windows. But having it can mean a very easy step to creating prototype code. Some prefer to use the DEBUG command and watch variables as they are modified through the DEBUGGER window.

It's all choices. Anyway, the Console Window opened by PB is somewhat limited in several respects. And it is slightly buggy, as the code below will indicate.

Code: Select all

;Program Name:  Resize Console
;Written By     Donald R. Darden
;Date:          April 8, 2004
;Copyright:     None retained.  Free for any and all purposes
;Liability:     None accepted, assumed, or allowed


Procedure.s string(length.w,chars.s)
temp.s=Space(length*Len(chars))
ReplaceString(temp,Space(Len(chars)),chars)
ProcedureReturn temp
EndProcedure


;This program demonstrates the ability to get the console handle and use it to resize the
;console window (within certain limits).  It gets the maximum size console that can be
;created with the current screens size (width x Height) and font size.  Then it progressively
;goes through the process of resizing the window, and putting number markers on the screen to
;measure the current heighth and width in character spaces.

;Note that as the program runs, it shows certain artifacts in the way it behaves.  For instance,
;although the maximum size on my PC at 800x600 is shown to be 46 lines of 100 characters, the
;console limit appears to be hardcoded at a maximum of 80 characters width using the Print(),
;PrintN(), and ConsoleLocate() commands in PB.

;Also note that although I can use ConsoleLocate() to extend the Print() and PrintN() range
;down to the bottommost line (maxline less 1),  The PrintN() command appears to be hardcoded
;to scroll when it reaches the normal sized console line limit -- the 25th line.

;Another artifact is that the right vertical scroll bar does not function as it should -- it
;does not scale in size to reflect how much of the used/maximum window appears, nor does it
;extend down to reflect the material that has scrolled up -- instead, it allows you to scroll
;down past the area that has been used, which serves no useful purpose.

;There are several issues that have not been addressed at this point.  One is the amount of
;memory set aside for the screen memory - you need enough to represent all the characters
;that can appear on the screen (Lines X Length).  You need a way to reposition the console
;window if need be, and you need a way to maximize/minimize the window.  And further, you
;have to realize that you are actually dealing with a viewport of the window -- not all of
;window is being displayed, though it may exist.  So issues of whether to wrap text to the
;viewable area or to the limits set by the actual window size have to be considered.  Generally,
;it is better if line limits are defined by the defined console window size, and just change
;the viewport by scrolling the view left or right, up or down.  But to do this, you need to
;look at using either high level or low level console commands, such as WriteConsole_() and
;ReadConsole_() (Look these up in the WIN32 External Help Section).




If OpenConsole() 
   ConsoleColor(15,1)
   ClearConsole()
   stdout.l=GetStdHandle_(#STD_OUTPUT_HANDLE)  
   *MaxSize.COORD 
   MS=GetLargestConsoleWindowSize_(stdout) 
   *MaxSize=@MS 
   Debug "Maximum X Coord = "+Str(*MaxSize\x-1) 
   Debug "Maximum Y Coord = "+Str(*MaxSize\y-1)
   NewSize.SMALL_RECT
   Scale.f=*MaxSize\x/*MaxSize\y
   For b=5 To *MaxSize\x-1
     a=Int(b/Scale)
     NewSize\Top=0
     NewSize\Left=0
     NewSize\Bottom=a
     NewSize\Right=b
     SetConsoleWindowInfo_(stdout,1,@NewSize)
     ConsoleLocate(1,1)
     Print(string(a,"+"))
     ConsoleLocate (1,1)
     For c=5 To b-1
       x.s=" "+Str(c)
       ConsoleLocate(0,0)
       Print(x)
       ConsoleLocate(c-Len(x)+1,0)
       Print(x) 
     Next
     x="  X,Y = "+Str(a)+","+Str(b)+"  "
     ConsoleLocate(b>>1-Len(x)>>1,a>>1)
     Print(x)
     ConsoleLocate(0,a)
     Print(Str(a))
     ConsoleLocate(b,a)
     Delay(500)
   Next 
   While Inkey()="" 
   Wend 
EndIf
has-been wanna-be (You may not agree with what I say, but it will make you think).
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Hardcoded at width of 80 characters.

Post by oldefoxx »

Though an 800x600 screen will probably max size at 100x46 console
window, you cannot resize the PB Console above 80x46. Again, it seems
to be a small bug.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Post Reply