AutoHotkey Community

It is currently May 26th, 2012, 1:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: January 2nd, 2009, 2:50 am 
Offline

Joined: May 28th, 2008, 2:11 am
Posts: 739
Location: Minnesota, USA
In the following script, I'm trying to use "SetConsoleCursorPosition" (Wrapped in MoveCursor()) to move the cursor around. The problem is, instead of setting the cursor to the correct coordinates, it always moves to 0,0 (Top left corner).
Code:
#Persistent
DllCall("AllocConsole")
hOutput := DllCall("GetStdHandle",UInt,-11)
DllCall("SetConsoleTitle",str,"Slanter's Console")
Write("SetConsoleCursorPosition hates me`n")
GetCursor(X,Y)
MsgBox Cursor is at %X%,%Y%
MoveCursor(2,2) ; Should move the cursor to 2,2
GetCursor(X,Y)
MsgBox Cursor is at %X%,%Y%
; This should be written starting at the second character of the second line. (Showing that GetCursor works correctly)
Write("Blah")

; ----------
; Functions
; ----------
Write(txt) {
   FileAppend, % txt, con
}
GetInput() {
   FileReadLine, txt, con, 1
   return txt
}
MoveCursor(X, Y) {
   Global hOutput
   VarSetCapacity(Coord, 4, 0)
   NumPut(Y, Coord, 0, "Short")
   NumPut(X, Coord, 2, "Short")
   If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, Coord))
      MsgBox %ErrorLevel%`n%A_LastError%
}
GetCursor(ByRef X, ByRef Y) {
   Global hOutput
   VarSetCapacity(BufferInfo, 22, 0)
   DllCall("GetConsoleScreenBufferInfo", UInt, hOutput, UInt, &BufferInfo)
   X := NumGet(BufferInfo, 4, "Short")
   Y := NumGet(BufferInfo, 6, "Short")
}

I have tried all of the following:
Code:
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, &Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int, &Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt64, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int64, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt *, Coord)
DllCall("SetConsoleCursorPosition", UInt, hOutput, Int *, Coord)


Any idea what I'm doing wrong?

_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2009, 3:31 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
There are two problems.
  1. Edit: The following was caused by SciTE redirecting StdOut:
    GetStdHandle(-11) does not return a handle to a console output buffer. This is why SetConsoleCursorPosition sets A_LastError to ERROR_INVALID_HANDLE=6. Use the following instead:
    Code:
    hOutput := DllCall("CreateFile" ,"str","CONOUT$" ,"uint",0xC0000000 ,"uint",2 ,"uint",0 ,"uint",3 ,"uint",0 ,"uint",0)
  2. SetConsoleCursorPosition expects a COORD structure. What you have is almost correct, but for the "UInt" type DllCall expects a string of numeric characters representing an unsigned integer, not a binary number. Also, X and Y are around the wrong way. Use one of the following instead:
    Code:
       VarSetCapacity(Coord, 4, 0)
       NumPut(X, Coord, 0, "Short")
       NumPut(Y, Coord, 2, "Short")
       If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, NumGet(Coord)))

    ; OR

       If (!DllCall("SetConsoleCursorPosition", UInt, hOutput, UInt, X | Y<<16))


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2009, 7:09 am 
Offline

Joined: May 28th, 2008, 2:11 am
Posts: 739
Location: Minnesota, USA
Great! Thanks! About the X and Y being switched, I think I did that to check if anything would happen (not really sure why I did that anymore tbh), then forgot to switch it back :oops:.

_________________
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Exabot [Bot], Google [Bot], Maestr0, poserpro, Yahoo [Bot] and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group