News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Getting client coordinates

Started by kellinka, November 21, 2013, 11:02:17 AM

Previous topic - Next topic

kellinka

Hello, I'm making a console program that involves clicking on specific locations.

In my code so far I'm able to get the mouse coordinates for the entire screen, but I only need the coordinates for the console window. I'm stuck working with the Irvine32 library and I'm having extreme difficulties including any Kernel32 libraries. I've spent about 6 hours trying to figure out how to take the coordinates I have for the mouse location and transpose them to be coordinates for the client window. I've looked into ScreenToClient but I can't figure out how to use it because of the second parameter call. I have no clue what the second parameter is in the Invoke ScreenToClient command is and I have no clue where it comes from or how to get it. I've seen a lot of people use something called HWND but I have no clue what that is or even how to get it. Some sites say it's some sort of handle to the window but I have no clue what that means. Is there a simple solution to this where I can convert the coordinates for the entire screen into the coordinates for the client I'm working with?


INCLUDE Irvine32.inc
INCLUDE Macros.inc
INCLUDE C:\masm32\include\user32.inc

.data
POINT STRUCT
x DWORD ?
y DWORD ?
POINT ENDS
MOUSE   POINT   <>

.code
GetMouseCoords PROC
.REPEAT
   invoke GetKeyState, VK_LBUTTON   
    .if sbyte ptr ah<0
      mov ebx, 1
   .ENDIF
.UNTIL(ebx==1)
   INVOKE   GetCursorPos, ADDR MOUSE
   ;here is where I would put the Invoke ScreenToClient command if I knew how to use it

   mov eax, MOUSE.x
   Call Writedec
   mov al, "X"
   Call WriteChar
   mov eax, MOUSE.Y
   Call Writedec
   mov eax, 10000
   Call Delay

ret
GetMouseCoords ENDP

dedndave

many of the functions you want to use may not appear in the Irvine32.lib library (or the INClude file)
you can make your own lib file - a bit advanced
or, you can use the ones in the masm32 package, rather than those in Irvine32

the parameters are pointers to (address of) RECT structures
RECT STRUCT
  left   DWORD ?
  top    DWORD ?
  right  DWORD ?
  bottom DWORD ?
RECT ENDS


HWND is a window handle
you can get the console window handle with GetConsoleWindow

dedndave

we've had a rash of people wanting to use the mouse with the console window, lately   :P

kellinka

GetConsoleWindow is what I needed!!!!! Thank you so much!

dedndave


jj2007

MS Paint (low-tech version):

include \masm32\MasmBasic\MasmBasic.inc        ; download
  Init
  mov ebx, rv(GetConsoleWindow)
  invoke ShowWindow, ebx, SW_RESTORE
  invoke MoveWindow, ebx, 200, 200, 824, 568, 1
  Print "Hold the left mousekey down and move the mouse - hit Escape to quit"
  .Repeat
        invoke Sleep, 1
        invoke GetKeyState, VK_LBUTTON        ; kind of undocumented but it works ;-)
        .if sbyte ptr ah<0
                push MouseY        ; create and fill...
                push MouseX        ; ... a POINT structure
                invoke ScreenToClient, ebx, esp
                pop eax        ; pt.y
                imul ecx, eax, 92        ; may need adjustment for different screen resolutions or console fonts
                sar ecx, 10        ; ecx=MouseX*92/2^10
                pop eax        ; pt.x
                imul edi, eax, 56        ; may need adjustment
                sar edi, 10        ; edi=MouseY*56/2^10
                Locate(ecx, Max(1, edi))
                Print "."        ; show the point
        .endif
        invoke GetKeyState, VK_ESCAPE
  .Until sbyte ptr ah<0
  Exit
end start

dedndave

here's my take on one that is compatible with Irvine32
although, i didn't need any Irvine functions - lol
i initially used WriteString, but replaced it with WriteFile   :P

i think it does a little better job calculating console position

jj2007

Quote from: dedndave on November 24, 2013, 12:42:17 AM
i think it does a little better job calculating console position

Indeed :t