December 11, 2024, 09:58:29 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Obtaining cursor position in console

Started by Peter, February 06, 2009, 05:40:03 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Peter

Finally I gave up. After hours of searching through the forums I must forfeit and ask for help.

I'm looking for equivalent functions for the old qbasic/quickbasic functions CSRLIN, POS and SCREEN.
SCREEN(1,1) would return the character on line 1, column 1. CSRLIN would simply return the line on which the cursor was currently, and POS would return the column the same way.

I've been trying to read the help to find info about this, but there is no crossreferencing to "related commands/functions" so it's really just a matter of guessing what it they might be called.
Either I wasn't clever enough to figure it out or they're missing. Help anyone?

Regards, Peter, Sweden.

Ionic Wind Support Team

Peter,
The console window is limited in capability, and not really equivalent to a DOS screen, in fact it is pretty much a simple text display device.

With that said you could do the cursor position, with a few API functions such as GetConsoleScreenBufferInfo which among other things returns the cursor coordinates in a packed dword.

Reading the character in the current console buffer is done with the API function ReadConsoleOutputCharacter.

The only question I have for you is why are you trying to get the cursor coordinates? 

Paul.

Ionic Wind Support Team

Peter

Well. I guess I could "keep track of it" manually, but it would just simplify things. I will be using Locate to write on different places (and different lengths) on the screen depending on user-decisions etc. I'll reconsider just trying to have a function noting where the cursor is, how much text I'm writing, and then calculating where it'll end up. Thank you for your quick reply.

GWS

Hi, I don't know if this might help.  It's one of Fletchie's bits of magic from long ago ..  :)

Sorry for having to zip it .. strangely, the Forum wouldn't let me post it in a 'code' block ..  ::)

all the best, :)

Graham
Tomorrow may be too late ..

Peter

Ah, the old wizards trick. May he rest in peace.

Exactly what I needed. Cheers!

Techno

Quote from: GWS on February 07, 2009, 02:33:33 AM
Hi, I don't know if this might help.  It's one of Fletchie's bits of magic from long ago ..  :)

Sorry for having to zip it .. strangely, the Forum wouldn't let me post it in a 'code' block ..  ::)

all the best, :)

Graham

Graham,

I try to compiled it in console mode with the IWB v3.0 Professional but it does not compiled it what wrong here



'I'm looking for equivalent functions for the old qbasic/quickbasic functions CSRLIN, POS and SCREEN.
'SCREEN(1,1) would return the character on line 1, column 1.
'CSRLIN would simply return the line on which the cursor was currently,
'and POS would return the column the same way.
'
'With that said you could do the cursor position, with a few API functions such as :
'GetConsoleScreenBufferInfo which among other things returns the cursor coordinates in a packed dword.
'Reading the character in the current console buffer is done with the API function ReadConsoleOutputCharacter.

DECLARE InitConsoleMisc()
DECLARE GetCurPos(y, x)
DECLARE GetConsoleChr(y, x)
DECLARE GetConsoleAtt(y, x, f, b)
declare "kernel32",GetStdHandle(Hnd:uint),int
declare "kernel32",GetConsoleScreenBufferInfo(hnd:int,bufinfo:memory),int
declare "kernel32",ReadConsoleOutputCharacterA(hnd:int,a$:string,le:int,c:int,co:pointer),int
declare "kernel32",ReadConsoleOutputAttribute(hnd:int,a$:string,le:int,c:int,co:pointer),int
declare GetCurPos(y:pointer,x:pointer)
declare GetConsoleChr(y:int,x:int)
declare GetConsoleAtt(y:int,x:int,f:pointer,b:pointer)

type info
def x:word
def y:word
def xc:word
def yc:word
def at:word
def le:char
def top:char
def ri:char
def bot:char
def xm:word
def ym:word
endtype

SETID "std_output_handle",0xfffffff5

def x,y:int
def rtn:string

openconsole 
print "This line and the line below will be repeated"
print "This line and the line above will be repeated"
print
for y=1 to 2
for x=1 to 80
print GetConsoleChr(y,x),
next x
next y
def f,b:int
locate 6,8
color 15,10
print "Color 15,10 - Test"
'GetConsoleAtt(6,8,f,b)

print "Foreground=",f
print "Background=",b
input rtn

closeconsole 

sub GetCurPos(y,x)
def in:info
def mem:memory
def n:int

   allocmem(mem,1,len(in))
   n=GetStdHandle(@std_output_handle)
   GetConsoleScreenBufferInfo(n,mem)
   readmem mem,1,in
   'returns zero based but locate is one based so add 1
   #x=in.xc+1
   #y=in.yc+1
   freemem mem
return

sub GetConsoleChr(y,x)
def r$:string
def nr,n:int

   n=GetStdHandle(@std_output_handle)
   ReadConsoleOutputCharacterA(n,r$,1,(y-1)*65536+x-1,nr)
return r$

sub GetConsoleAtt(y,x,f,b)
def r$:string
def nr,n:int

   n=GetStdHandle(@std_output_handle)
   ReadConsoleOutputAttribute(n,r$,1,(y-1)*65536+x-1,nr)

   If r$=""
#f=0
      #b=0
   else
'foreground in low nibble
      'background in high nibble
      #f=asc(r$) & 15
      #b=asc(r$) / 16
   endif
return

Egil

You try to do it more complicated than neexded.... ;)

I modified Graham's /Fletchie's example a little, and now it prints the cursor location. But not sure if it needs a little more work.

'I don't know if this might help - it's one of Fletchie's bits of magic from long ago ..  :)

'______________________________________________________________________

gosub InitConsoleMisc

def x,y:int
def rtn:string

openconsole 

print "This line and the line below will be repeated"
print "This line and the line above will be repeated"
print
for y=1 to 2
for x=1 to 80
print GetConsoleChr(y,x),
next x
next y
def f,b:int
locate 6,8
color 15,10
print "Color 15,10 - Test"
GetConsoleAtt(6,8,f,b)

print "Foreground=",f
print "Background=",b



' Lines inserted
'**********************************************************

color 7,0
print:print
GetCurPos(y,x)
print "Cursor X,Y now: ", x,y

'**********************************************************



input rtn

closeconsole 

end

sub InitConsoleMisc

declare "kernel32",GetStdHandle(Hnd:uint),int
declare "kernel32",GetConsoleScreenBufferInfo(hnd:int,bufinfo:memory),int
declare "kernel32",ReadConsoleOutputCharacterA(hnd:int,a$:string,le:int,c:int,co:pointer),int
declare "kernel32",ReadConsoleOutputAttribute(hnd:int,a$:string,le:int,c:int,co:pointer),int
declare GetCurPos(y:pointer,x:pointer)
declare GetConsoleChr(y:int,x:int)
declare GetConsoleAtt(y:int,x:int,f:pointer,b:pointer)
type info
def x:word
def y:word
def xc:word
def yc:word
def at:word
def le:char
def top:char
def ri:char
def bot:char
def xm:word
def ym:word
endtype

setid "std_output_handle",0xfffffff5

return

sub GetCurPos(y,x)

def in:info
def mem:memory
def n:int

      allocmem(mem,1,len(in))

      n=GetStdHandle(@std_output_handle)
      GetConsoleScreenBufferInfo(n,mem)
      readmem mem,1,in
      'returns zero based but locate is one based so add 1
      #x=in.xc+1
      #y=in.yc+1
      freemem mem

return

sub GetConsoleChr(y,x)

def r$:string
def nr,n:int

      n=GetStdHandle(@std_output_handle)
      ReadConsoleOutputCharacterA(n,r$,1,(y-1)*65536+x-1,nr)

return r$

sub GetConsoleAtt(y,x,f,b)

def r$:string
def nr,n:int

      n=GetStdHandle(@std_output_handle)
      ReadConsoleOutputAttribute(n,r$,1,(y-1)*65536+x-1,nr)

      if r$=""
            #f=0
            #b=0
      else
            'foreground in low nibble
            'background in high nibble
            #f=asc(r$)&15
            #b=asc(r$)/16
      endif

return

'_______________________________________________________________________________

'all the best, :)

'Graham
Support Amateur Radio  -  Have a ham  for dinner!