Sign in to follow this  
Followers 0
Pottery

_MouseGetWinPos()

7 posts in this topic

#1 ·  Posted (edited)

Little snippet I made for a project, thought I'd share it. If there is already a function like this I didn't see it.

It gives you your current mouse coordinates according to the window's x,y position.

Function:

Func _MouseGetWinPos($hWnd)
    Global $WinPos = WinGetPos($hWnd)
    Global $Pos = MouseGetPos()
    $Pos[0] = $Pos[0] - $WinPos[0] - 2 ; -2 for the border line
    $Pos[1] = $Pos[1] - $WinPos[1] - 21 ; -21 for the title bar
EndFunc

Showing it works:

#include "GUIConstantsEx.au3"
#include "Misc.au3"

$hGUI = GUICreate('Test', 200, 200, -1, -1)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    _MouseGetWinPos($hGUI)
    ToolTip($Pos[0] & ', ' & $Pos[1])
WEnd
Edited by Epdmk

Share this post


Link to post
Share on other sites



Little snippet I made for a project, thought I'd share it. If there is already a function like this I didn't see it.

It gives you your current mouse coordinates according to the window's x,y position.

Function:

Func _MouseGetWinPos($hWnd)
    Global $WinPos = WinGetPos($hWnd)
    Global $Pos = MouseGetPos()
    $Pos[0] = $Pos[0] - $WinPos[0] - 2 ; -2 for the border line
    $Pos[1] = $Pos[1] - $WinPos[1] - 21 ; -21 for the title bar
EndFunc
...

Global: Must be in the beginning of the code!

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

It works, try it before posting..

If you do it without global, it doesn't work.

It SHOULD be in the beginning, doesn't HAVE to be.

Share this post


Link to post
Share on other sites

It works, try it before posting..

If you do it without global, it doesn't work.

It SHOULD be in the beginning, doesn't HAVE to be.

I did not say that your code does not work! I just learned that Global variables must be declared in the scopo of the program. Ok, keep writing their code so, when you encounter an error, you'll understand what I said...

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

I'll wager your borderline and titlebar figures will vary depending upon the screen resolution and system font chosen.

Share this post


Link to post
Share on other sites

;MouseCoordMode
;Sets the way coords are used in the mouse functions, either absolute coords or coords relative to the current active window:
;0 = relative coords to the active window
;1 = absolute screen coordinates (default)
;2 = relative coords to the client area of the active window


$a = Opt("MouseCoordMode") ;save mode

$hgui = GUICreate("MouseCoordMode") ;gui
$mode0 = GUICtrlCreateLabel("", 10, 10, 300, 30) ;coord-mode 0
$mode1 = GUICtrlCreateLabel("", 10, 50, 300, 30) ;coord-mode 1
$mode2 = GUICtrlCreateLabel("", 10, 100, 300, 30) ;coord-mode 2
GUISetState() ;show gui
Do ;loop until exit
    For $i = 0 To 2 ;all modes
        Opt("MouseCoordMode", $i) ;set mode
        GUICtrlSetData(Eval("mode" & $i), "Coordmode " & $i & "   x=" & MouseGetPos(0) & "   Y=" & MouseGetPos(1)) ;set label with coords depending on mode
    Next
Until GUIGetMsg() = -3 ; exit

Opt("MouseCoordMode", $a) ;restore mode

Share this post


Link to post
Share on other sites

Global variables should ALWAYS be declared at the beginning, even if you are going to use them later. You're right, they don't HAVE to be, but definately should be. Just declare them at the beginning and use them later. As far as the code goes, AndyG is right. The best way to find the mouse coordinates relative to the client area of the gui is to use MouseCoordMode. Here is an example of how the code should be written:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate('Test', 200, 200)
GUISetState()

While 1
    $nMsg = GUIGetMsg()

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $MousePos = _MouseGetWinPos($hGUI)
    ToolTip($MousePos[0] & ', ' & $MousePos[1])
WEnd

Func _MouseGetWinPos($hWnd)
    $Opt = Opt("MouseCoordMode", 2)
    $Pos = MouseGetPos()
    Opt("MouseCoordMode", $Opt)
    Return $Pos
EndFunc ;==>_MouseGetWinPos2

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
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
Sign in to follow this  
Followers 0