!This standalone program demonstrates some manipulation of the
!console screen and buffer.  The same routines can also
!be used in a dll.  See the sample QuickWin\ConApp.
!Compile as a console program:
!f90 console.f90

Program Console
use dflib
use dfwin

!In DevStudio Help Index look up WriteConsole, and then from the
!list of console functions in the See Also list, look up
! GetConsoleScreenBufferInfo and
! SetConsoleCursorPosition  
!to understand what is being done here.

     integer fhandle, nlines, ncols
     logical lstat
     Type(T_COORD) wpos
	 Type(T_SMALL_RECT) sr
     Type(T_CONSOLE_SCREEN_BUFFER_INFO) cinfo
     fhandle = GetStdHandle(STD_OUTPUT_HANDLE)

!Your console is automatically provided with a
!console application so you don't need to allocate
!another one.   

!To avoid an error, you need to avoid setting
!a console window greater in size than the 
!screen buffer.  Likewise, you need to avoid
!setting the size of the console buffer less
!than console screen.
!
!You can set the default properties of console
!command prompts to allow the following code to
!work.
!
!Or you can use GetConsoleScreenBufferSize and
!GetConsoleWindowInfo to see what the current size
!limits are, and change them accordingly. If you
!want to increase the size of the console window,
!you would get the size of the console screen buffer
!and increase it if necessary so that it will be
!bigger than or equal to the desired new console window.

!Set the size of the console window
     sr.top    =   0 
	 sr.left   =   0
	 sr.bottom =   40 ! <= console buffer height -1
	 sr.right  =   60 ! <= console buffer width  -1
     lstat = SetConsoleWindowInfo(fhandle, .TRUE., sr)

     nlines = 100
	 ncols  =  80
!Set size of the console buffer
     wpos.x = ncols  ! columns >= console window width
     wpos.y = nlines ! lines   >= console window height
     lstat = SetConsoleScreenBufferSize(fhandle, wpos)

!Write blank lines
     wpos.x = 0
     wpos.y = 0
     lstat = SetConsoleCursorPosition(fhandle, wpos)
     do i = 0, nlines-1
     write(*,*) Repeat(" ",ncols)
     end do

!Go back to the top
     wpos.x = 0
     wpos.y = 0
     lstat = SetConsoleCursorPosition(fhandle, wpos)

!Position and write two lines
     wpos.x = 5 ! 6 characters from left 
     wpos.y = 5 ! 6 lines down
     lstat = SetConsoleCursorPosition(fhandle, wpos)
     write(*,*) 'Six across Six down'

     wpos.x = 15 ! 16 characters from left 
     wpos.y = 10 ! 11 lines down
     lstat = SetConsoleCursorPosition(fhandle, wpos)
     write(*,*) 'Sixteen  across Eleven down'

     read(*,*)
end program Console
