 use Kernel32
 Implicit None
  Integer(kind=4), Parameter                      :: DefIn = 5
  Integer(kind=4), Parameter                      :: DefOut = 6 
! ******************************************************************************
! *                          CONSOLE VARIABLES
  Type ConsoleRec 
    Logical(kind=4)                              :: Opened                     ! SCR_OpenConsole was Called
    Integer (Handle)                             :: InputHandle                ! Device hadle - Standard Input
    Integer (Handle)                             :: OutputHandle               !              - Standard Output
    Integer (Handle)                             :: ErrorHandle                !              - Standard error

    Type (T_INPUT_RECORD)                        :: InputlpBuffer              ! INPUT_RECORD lpBuffer
    Type (T_CONSOLE_SCREEN_BUFFER_INFO)          :: lpConsoleScreenBufferInfo  ! PCONSOLE_SCREEN_BUFFER_INFO
    Type (T_SMALL_RECT)                          :: lpConsoleWindow            ! SMALL_RECT* lpConsoleWindow
    Type (T_OPENFILENAME)                        :: Ofn                        ! 
    Type (T_COORD)                               :: Csbi                       ! Cursor position: X-columns; Y-rows
    Type (T_OSVERSIONINFO)                       :: lpVersionInformation       ! LPOSVERSIONINFOA lpVersionInformation
    Integer(DWORD)                               :: nLength                    ! DWORD nLength
    Integer(LPDWORD)                             :: lpNumberOfEventsRead       ! LPDWORD 

                                                                               ! Cosole Window 
    Integer(kind=4)                              :: Row                        ! Row    - current position
    Integer(kind=4)                              :: MaxRow                     !        - boundary
    Integer(kind=4)                              :: Col                        ! Col    - current position
    Integer(kind=4)                              :: MaxCol                     !        - boundary
    
                                                                               ! Consol Buffer
    Integer(kind=4)                              :: BufRow                     ! Row    - current position
    Integer(kind=4)                              :: BufMaxRow                  !        - boundary
    Integer(kind=4)                              :: BufCol                     ! Col    - current position
    Integer(kind=4)                              :: BufMaxCol                  !        - boundary

    Integer(kind=4)                              :: WinMaxRow                  ! window max size
    Integer(kind=4)                              :: WinMaxCol                  !

    Character(len=132)                           :: ConsoleTitle               ! Title
    Character(len=132)                           :: OldConsoleTitle           ! Old (original) Title 

    Integer(LPWORD)                              :: Attributes                 ! Text Attributes
    Integer(LPWORD)                              :: OldAttributes              ! Text Attributes
    Integer(kind=2)                              :: ForeGround                 ! [0-15] see SCR_FOREGROUND_?????? colours
    Integer(kind=2)                              :: BackGround                 ! [0-15] see SCR_BACKGROUND_?????? colours
  End Type
  Type (ConsoleRec)                              :: SCR                        ! Screen console structure

  Character(len=*), Parameter                    :: Subr = 'TkLibTest'
  Logical(kind=4)                                :: IoStat
  Integer(DWORD)                                 :: nLength
  Type (T_COORD)                                 :: dwWriteCoord
  Integer(LPDWORD)                               :: lpNumberOfCharsWritten 
  Integer(LPDWORD)                               :: NumberOfChars = 0
  Character(Len=132)                             :: String

  String =  ' * * * '//Trim(Subr)//' * * * '

  SCR%OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE)
  IoStat = GetConsoleScreenBufferInfo(SCR%OutputHandle, SCR%lpConsoleScreenBufferInfo)
  SCR%MaxRow = SCR%lpConsoleScreenBufferInfo%SrWindow%Bottom - SCR%lpConsoleScreenBufferInfo%srWindow%Top                
  SCR%MaxCol = SCR%lpConsoleScreenBufferInfo%SrWindow%Right  - SCR%lpConsoleScreenBufferInfo%srWindow%Left                    
  SCR%BufMaxRow = SCR%lpConsoleScreenBufferInfo%DwSize%Y
  SCR%BufMaxCol = SCR%lpConsoleScreenBufferInfo%DwSize%X
  SCR%Row = SCR%Csbi%Y-SCR%lpConsoleScreenBufferInfo%srWindow%Top 
  SCR%Col = SCR%Csbi%X-SCR%lpConsoleScreenBufferInfo%srWindow%Left
  SCR%Csbi%Y = SCR%lpConsoleScreenBufferInfo%DwCursorPosition%Y
  SCR%Csbi%X = SCR%lpConsoleScreenBufferInfo%DwCursorPosition%X
  SCR%BufRow = SCR%lpConsoleScreenBufferInfo%DwCursorPosition%Y
  SCR%BufCol = SCR%lpConsoleScreenBufferInfo%DwCursorPosition%X
  SCR%Attributes = SCR%lpConsoleScreenBufferInfo%wAttributes
  SCR%ForeGround = Mod(SCR%Attributes,16)
  SCR%BackGround = SCR%Attributes-SCR%ForeGround

  SCR%Row = 5
  SCR%Col = 0
  
  nLength = Len_Trim(String)
  SCR%OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE)
  dwWriteCoord%X = SCR%Col
  dwWriteCoord%Y = SCR%Row
  IoStat = WriteConsoleOutputAttribute(SCR%OutputHandle, Loc(SCR%Attributes), nLength, dwWriteCoord, Loc(NumberOfChars))
  IoStat = WriteConsole(SCR%OutputHandle, Loc(String), Len_Trim(String), Loc(NumberOfChars), 0)

  Stop
  End
 