Jump to content

Mouse Position on Active Window


Lee Bussy
 Share

Recommended Posts

In this thread I asked about an applet to get the x,y position of the current window. I was directed by alexmadman to "AutoIt Window Info" which solved my immediate issue. Didonet posted a script example as well which did not solve my immediate issue but left me with an idea for an enhancement to my own code. Here is the code I am working with:

#include <GUIConstants.au3>

$frmX_Y = GUICreate(" ", 120, 60, -1, -1, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$lblX = GUICtrlCreateLabel("X Position = ", 16, 8, 63, 17)
$lblY = GUICtrlCreateLabel("Y Position = ", 16, 32, 63, 17)
$lblX_Val = GUICtrlCreateLabel("0000", 80, 8, 28, 17)
$lblY_Val = GUICtrlCreateLabel("0000", 80, 32, 28, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        EndSwitch
        
    GuiCtrlSetData($lblX_Val, MouseGetPos(0))
    GuiCtrlSetData($lblY_Val, MouseGetPos(1))
WEnd

I need to make it display the x,y of the current window. I had thought changing instances of MouseGetPos() to GUIGetCursorInfo() would do it but that seems to break things rather solidly.

I'm sure I'm missing something simple, anyone care to give me a hint what that is?

Edited by Lee Bussy
Link to comment
Share on other sites

Humm like this?

#include <GUIConstants.au3>
Opt("WinTitleMatchMode",2)
GUICreate("", 100, 100, -1, -1, -1, $WS_EX_TOPMOST)
GUISetState ()

GuiCtrlCreateLabel("Mouse X - ", 5, 5)
GuiCtrlCreateLabel("Mouse Y - ", 5, 20)
GuiCtrlCreateLabel("Win X - ", 5, 35)
GuiCtrlCreateLabel("Win Y - ", 5, 50)
$x = GuiCtrlCreateLabel("", 55, 5, 50, 15)
$y = GuiCtrlCreateLabel("", 55, 20, 50, 15)
$WinPosx = GuiCtrlCreateLabel("", 55, 35, 50, 15)
$WinPosy = GuiCtrlCreateLabel("", 55, 50, 50, 15)

$x_pos1 = MouseGetPos(0)
$y_pos1 = MouseGetPos(1)

While 1
Sleep(10)
$msg = GUIGetMsg()
Global $x
Global $y
Global $x_pos1
Global $x_pos2
$x_pos = MouseGetPos(0)
$y_pos = MouseGetPos(1)
$WinPos=WinGetPos("")
GUICtrlSetData($WinPosX,$WinPos[0])
GUICtrlSetData($WinPosY,$WinPos[1])
    If $x_pos <> $x_pos1 Then
    GuiCtrlSetData($x, MouseGetPos(0))
    $x_pos1 = MouseGetPos(0)
    EndIf

    If $y_pos <> $y_pos1 Then
    GuiCtrlSetData($y, MouseGetPos(1))
    $y_pos1 = MouseGetPos(1)
    EndIf
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

It tells you the postition of the active window.

Link to comment
Share on other sites

Yeah, I was gonna say your script was unnecessary long.

#include <GUIConstants.au3>
Opt("WinTitleMatchMode",2)
GUICreate("", 100, 100, -1, -1, -1, $WS_EX_TOPMOST)
GUISetState ()
GuiCtrlCreateLabel("Mouse X - ", 5, 5)
GuiCtrlCreateLabel("Mouse Y - ", 5, 20)
GuiCtrlCreateLabel("Win X - ", 5, 35)
GuiCtrlCreateLabel("Win Y - ", 5, 50)
$x = GuiCtrlCreateLabel("", 55, 5, 50, 15)
$y = GuiCtrlCreateLabel("", 55, 20, 50, 15)
$WinPosx = GuiCtrlCreateLabel("", 55, 35, 50, 15)
$WinPosy = GuiCtrlCreateLabel("", 55, 50, 50, 15)

While 1
Sleep(25)
$msg = GUIGetMsg()
$Mousepos = MouseGetPos()
$WinPos=WinGetPos("")
GuiCtrlSetData($x,$Mousepos[0])
GuiCtrlSetData($y,$Mousepos[1])
GUICtrlSetData($WinPosX,$WinPos[0])
GUICtrlSetData($WinPosY,$WinPos[1])
If $msg = $GUI_EVENT_CLOSE Then
    Exit
EndIf
WEnd
Link to comment
Share on other sites

Okay that wasn't quite what I meant but you put me on the right track ... I wanted the position of the mouse within (or relative to) the current window. Here's what I came up with based on your notes:

#include <GUIConstants.au3>

$frmX_Y = GUICreate(" ", 120, 60, -1, -1, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$lblX = GUICtrlCreateLabel("X Position = ", 16, 8, 63, 17)
$lblY = GUICtrlCreateLabel("Y Position = ", 16, 32, 63, 17)
$lblX_Val = GUICtrlCreateLabel("0000", 80, 8, 28, 17)
$lblY_Val = GUICtrlCreateLabel("0000", 80, 32, 28, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        EndSwitch

    $WinPos=WinGetPos("")
    GuiCtrlSetData($lblX_Val, MouseGetPos(0) - $WinPos[0])
    GuiCtrlSetData($lblY_Val, MouseGetPos(1) - $WinPos[1])

    Sleep(10)
WEnd

So this is almost what I want. I can make this work based on some assumptions, but in a perfect world I would have the x,y of the mouse within the active part of the window (without the borders or top bar). Is there a function to actually return the position within the window without calculating it from the window position and the x,y on the screen? If so is there an option to return the coordinates within the active part of the window, ignoring the borders and bar?

Link to comment
Share on other sites

RTFM FFS.

If you don't want to answer, don't. Throwing an RTFM out there is really less than helpful and just makes you look like an impatient ass. I know all of the answers are in the manual but sometimes if you don't know what it is you are looking for, you don't know it when you see it. Not every language addresses things in the same manner, so I was not looking for an option that gets set in the header of the script independent of the function call

As you were digging deep for your coarse reply, I was re-reading the help file based on the actual help I received from Nahuel. Here's what I came up with which does give me what I need:

#include <GUIConstants.au3>

AutoItSetOption("MouseCoordMode", 2)

$frmX_Y = GUICreate(" ", 120, 60, -1, -1, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$lblX = GUICtrlCreateLabel("X Position = ", 16, 8, 63, 17)
$lblY = GUICtrlCreateLabel("Y Position = ", 16, 32, 63, 17)
$lblX_Val = GUICtrlCreateLabel("0000", 80, 8, 28, 17)
$lblY_Val = GUICtrlCreateLabel("0000", 80, 32, 28, 17)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        EndSwitch

    GuiCtrlSetData($lblX_Val, MouseGetPos(0))
    GuiCtrlSetData($lblY_Val, MouseGetPos(1))
    
    Sleep(10)
WEnd

I've been in the IT industry for, oh, about 25 years. The best advice I was ever given, which is NOT in the manual, and which I will share with you freely is this: If you feel like you must demean a person before offering advice, skip both.

Nahuel, thanks very much for pointing me in the right direction.

Edited by Lee Bussy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...