Pottery Posted July 29, 2010 Share Posted July 29, 2010 (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 EndFuncShowing 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 July 29, 2010 by Epdmk Link to comment Share on other sites More sharing options...
JScript Posted July 29, 2010 Share Posted July 29, 2010 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 Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
Pottery Posted July 29, 2010 Author Share Posted July 29, 2010 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. Link to comment Share on other sites More sharing options...
JScript Posted July 29, 2010 Share Posted July 29, 2010 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 Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
PartyPooper Posted July 29, 2010 Share Posted July 29, 2010 I'll wager your borderline and titlebar figures will vary depending upon the screen resolution and system font chosen. Link to comment Share on other sites More sharing options...
AndyG Posted July 29, 2010 Share Posted July 29, 2010 ;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 Link to comment Share on other sites More sharing options...
dantay9 Posted July 30, 2010 Share Posted July 30, 2010 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now