module winconsole
use, intrinsic:: ISO_C_BINDING
type, bind(C):: COORD
integer(C_SHORT) X
integer(C_SHORT) Y
end type COORD
integer (C_LONG), parameter:: STD_INPUT_HANDLE = -10
integer (C_LONG), parameter:: STD_OUTPUT_HANDLE = -11
integer (C_LONG), parameter:: STD_ERROR_HANDLE = -12
interface
function GetStdHandle(nStdHandle) &
bind (C, name='GetStdHandle')
import
!GCC$ attributes dllexport, stdcall :: GetStdHandle
integer (C_INTPTR_T) GetStdHandle
integer (C_LONG), value:: nStdHandle
end function GetStdHandle
function SetConsoleCursorPosition (hConsoleOutput, dwCursorPosition ) &
bind (C, name='SetConsoleCursorPosition')
import
!GCC$ attributes dllexport, stdcall :: SetConsoleCursorPosition
integer(C_INT) SetConsoleCursorPosition
integer(C_INT), value:: hConsoleOutput
type(COORD), value:: dwCursorPosition
end function SetConsoleCursorPosition
function WriteFile (hConsoleOutput ,lpCharacter ,nLength , lpNumberOfCharsWritten, overlapped ) &
bind (C, name='WriteFile')
import
!GCC$ attributes dllexport, stdcall :: WriteFile
integer (C_INT) WriteFile
integer (C_INT), value:: hConsoleOutput
character(kind=C_CHAR):: lpCharacter(1)
integer (C_INT), value:: nLength
integer (C_INT) lpNumberOfCharsWritten
integer (C_INT) overlapped
end function WriteFile
function GetLastError() bind(C, name='GetLastError')
import
!GCC$ attributes dllexport, stdcall:: GetLastError
integer (C_INT) GetLastError
end function GetLastError
end interface
contains
end module winconsole
program cursorg
use, intrinsic :: ISO_C_BINDING
use winconsole
integer(C_LONG):: ires, written, leng
integer(C_INTPTR_T):: hout
type(COORD) posn
integer, parameter:: TITLE_LEN = 80
character(len=TITLE_LEN):: str
hout = GetStdHandle(STD_OUTPUT_HANDLE)
if (hout .eq. -1) then
ires = GetLastError()
print *, ires
stop
end if
posn%x = 40
posn%y = 10
str = 'Hey it works'
ires = SetConsoleCursorPosition(hout, posn)
leng = len_trim(str)
ires = WriteFile(hout, str, leng, written, 0)
read (*,*) str
end program