Jump to content

how can i get the mouse position


jennico
 Share

Recommended Posts

hello,

i need to know the mouse position while a window is being dragged, but it seems that mousegetpos() doesn`t update the data, while the autoit window tool does. i tried to decompile the autoit window tool, but not possible, so i can't get the function.

the example shows that neither the window position is being updated.

i could imagine that the script might be paused while moving a window. (?)

Here is an example:

#include <GuiConstants.au3>

$GUI = GUICreate("Mouse Position Example", 392, 323)
$label = GUICtrlCreateLabel("",0,0,200,200)

GUISetState()

While 1
    $pos = MouseGetPos()
    $control = WinGetPos($GUI)
    GUICtrlSetData($label,"x "&$pos[0]&" - y "&$pos[1]&@LF&"x "&$control[0]&" - y "&$control[1])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Exit

while moving the window, the data doesn't change.

so please does anyone know what to do ? maybe a dllcall might help ?

thx j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

hello,

i need to know the mouse position while a window is being dragged, but it seems that mousegetpos() doesn`t update the data, while the autoit window tool does. i tried to decompile the autoit window tool, but not possible, so i can't get the function.

the example shows that neither the window position is being updated.

i could imagine that the script might be paused while moving a window. (?)

Here is an example:

#include <GuiConstants.au3>

$GUI = GUICreate("Mouse Position Example", 392, 323)
$label = GUICtrlCreateLabel("",0,0,200,200)

GUISetState()

While 1
    $pos = MouseGetPos()
    $control = WinGetPos($GUI)
    GUICtrlSetData($label,"x "&$pos[0]&" - y "&$pos[1]&@LF&"x "&$control[0]&" - y "&$control[1])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Exit

while moving the window, the data doesn't change.

so please does anyone know what to do ? maybe a dllcall might help ?

thx j.

#include <GuiConstants.au3>
#include <misc.au3>
Global Const $WM_NCHITTEST = 0x0084
$GUI = GUICreate("Mouse Position Example", 392, 323)
$label = GUICtrlCreateLabel("",0,0,200,200)

GUISetState()
GUIRegisterMsg($WM_NCHITTEST,"WM_NCHITTEST")
$dragging = False
While 1
    $pos = MouseGetPos()
    $control = WinGetPos($GUI)
    GUICtrlSetData($label,"x "&$pos[0]&" - y "&$pos[1]&@LF&"x "&$control[0]&" - y "&$control[1])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
    If $dragging Then
        While _IsPressed("1")
        $pos2 = MouseGetPos()
        $control2 = WinGetPos($GUI)
        WinMove("Mouse Position Example","",$control[0] -$pos[0] + $pos2[0],$control[1] - $pos[1] + $pos2[1])
        GUICtrlSetData($label,"x "&$pos2[0]&" - y "&$pos2[1]&@LF&"x "&$control2[0]&" - y "&$control2[1])
        WEnd
    EndIf
    
WEnd

Exit

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If $hWnd = $GUI And $iMsg = $WM_NCHITTEST Then
     $id = _API_DefWindowProc($hWnd, $iMsg, $iwParam, $ilParam)
     If $id = 2 Then;if it is the title handle
         $dragging = True
       Return 1;return the client handle
   Else
       $dragging = False
       Return $id;return the real handle
   EndIf
   EndIf
          
EndFunc
  
Func _API_DefWindowProc($hWnd, $iMsg, $iwParam, $ilParam)
  Local $aResult

  $aResult = DllCall("User32.dll", "int", "DefWindowProc", "hwnd", $hWnd, "int", $iMsg, "int", $iwParam, "int", $ilParam)
  Return $aResult[0]
EndFunc

EDIT: For got to say, might not work in some setups for Vista and maybe non standard themes.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

excellent, it works !

:-)

i will see if i understand the code and if i can't integrate it, i return once more.

thank you !!!!

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

ok, i have some difficulties integrating the code.

the window is jumping and when i drag another window (like firefox), my window is being dragged in the same way.

the problem could be that i didn't use the guiregistermsg in my scipt, instead i just use the guigetmsg from controls and now there seem to be interferences introducing the guiregistermsg method. maybe you could workout a code that follows the scheme:

$msg=guigetmsg()

if winactive($gui) then............follow the code.........(???)

or maybe deregister the WM_NCHITTEST function when not active ???

thx

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

well, i managed the jumping window problem by adding short pauses.

but the other more serious problem is the cpu usage that rises up to 100% on dragging the window !

so i would be glad if anyone could improve the code or has a different idea .

thx j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

Hi, an adaption of both your codes with a twist of mine..lol

Cpu friendly with 1 small sleep in the drag process, could probly eliminate that sleep as well by using check for state change instead.

#include <GUIConstants.au3>
#include <misc.au3>
Global Const $WM_NCLBUTTONDOWN = 0xA1
Global $Clicked = 0, $Offset[2], $Xpos, $Ypos, $Xcontrol, $Ycontrol

$GUI = GUICreate("Mouse Position Example", 392, 323)
$label = GUICtrlCreateLabel("",5,5,110,30)
GUISetState()

GUIRegisterMsg ($WM_NCLBUTTONDOWN, "WM_NCLBUTTONDOWN_FUNC")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case Else
            ;;;
    EndSelect
    UpdateLabel()
WEnd

Func UpdateLabel()
    $pos = MouseGetPos()
    $control = WinGetPos($GUI)
    If $pos[0] <> $Xpos Or $pos[1] <> $Ypos Then 
        GUICtrlSetData($label,"Mouse: x " & $pos[0] & " - y " & $pos[1] & @LF & _
                                "GUI:      x " & $control[0] & " - y " & $control[1])
        $Xpos = $pos[0]
        $Ypos = $pos[1]
    EndIf   
    If $control[0] <> $Xcontrol Or $control[1] <> $Ycontrol Then
        GUICtrlSetData($label,"Mouse: x " & $pos[0] & " - y " & $pos[1] & @LF & _
                                "GUI:      x " & $control[0] & " - y " & $control[1])
        $Xcontrol = $control[0]
        $Ycontrol = $control[1]
    EndIf   
    If $Clicked = 1 Then
        While _IsPressed("1")
            Sleep(10)
            $pos2 = MouseGetPos()
            $control2 = WinGetPos($GUI)
            WinMove($GUI,"", $Offset[0] + $pos2[0], $Offset[1] + $pos2[1])
            GUICtrlSetData($label,"Mouse: x " & $pos2[0] & " - y " & $pos2[1] & @LF & _
                                    "GUI:      x " & $control2[0] & " - y " & $control2[1])
        WEnd
        $Clicked = 0
    EndIf
EndFunc 

Func WM_NCLBUTTONDOWN_FUNC($hWnd, $Msg, $WParam, $LParam)
    Local $nID            = BitAnd($wParam, 0x0000FFFF)
    Dim $MGP, $WGP
    If $Clicked = 0 And $nID = 2 And $hWnd = $GUI Then
        $MGP = MouseGetPos()
        $WGP = WinGetPos($hWnd)
        $Offset[0] = $WGP[0] - $MGP[0]
        $Offset[1] = $WGP[1] - $MGP[1]
        $Clicked = 1
        Return 1
    Else 
        Return $GUI_RUNDEFMSG
    EndIf   
EndFunc

Now if only my windows XP was as cpu friendly when dragging normal windows around.

(my windows xp chews alot cpu time if I grab a window and wobble it, and I don't mean with code or scripts either, just normal xp use) o_0 :)

Cheers

Edited by smashly
Link to comment
Share on other sites

going to try that thx.

my xp cpu doesn't react at all on dragging windows maybe because i turned off "show windows content on dragging" in performance options. that could make the difference i guess.

j.

btw: just one thing i changed in martin'code as well:

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If $hWnd = $child And $iMsg = $WM_NCHITTEST Then
        $id = _API_DefWindowProc($hWnd, $iMsg, $iwParam, $ilParam)
        If $id = 2 Then;if it is the title handle
            $dragging = True
            Return 1;return the client handle
        Else
            $dragging = False
            Return $id;return the real handle
        EndIf
    Else
        Return $GUI_RUNDEFMSG 
    EndIf
EndFunc

i think Return $GUI_RUNDEFMSG is something quite important....

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

huh.. so why do you need the label updated while dragging? o_0

I actually set the Show windows contents, just to write the example... :)

I can shorten that example by at least 15 lines or more if your not needing the data to be presented on screen...lol

Cheers

Link to comment
Share on other sites

sry, i don't need the label !

i just introduced the label for demonstration reasons. i just need the data (winpos and mousepos) in order to be able to react on a certain window position.

so without label update i am completely satisfied !

j.

i will explain more exactly what i intent: i have a column like window ( like 80x750 ). when the user moves the window towards the screen border, i want the window turn automatically into a vertical one ( 750x80 ) and vice versa. so i just need mouse and window position, moreover winactive and _ispressed in order to determine the state and then perform the winmove operation from vertical to horizontal.

i actually don't need the label !

j. :-)

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

going to try that thx.

my xp cpu doesn't react at all on dragging windows maybe because i turned off "show windows content on dragging" in performance options. that could make the difference i guess.

j.

btw: just one thing i changed in martin'code as well:

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If $hWnd = $child And $iMsg = $WM_NCHITTEST Then
        $id = _API_DefWindowProc($hWnd, $iMsg, $iwParam, $ilParam)
        If $id = 2 Then;if it is the title handle
            $dragging = True
            Return 1;return the client handle
        Else
            $dragging = False
            Return $id;return the real handle
        EndIf
    Else
        Return $GUI_RUNDEFMSG 
    EndIf
EndFunc

i think Return $GUI_RUNDEFMSG is something quite important....

You don't need the line $id = ..., it gives the value of $GUI_RUNDEFMSG to $id. $GUI_RUNDEFMSG is better and I should have used that in the first place.

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If $hWnd = $child And $iMsg = $WM_NCHITTEST Then
        
        If $RUNDEFMSG = 2 Then;if it is the title handle
            $dragging = True
            Return 1;return the client handle
        Else
            $dragging = False
            Return $RUNDEFMSG;return the real handle
        EndIf
    Else
        Return $GUI_RUNDEFMSG 
    EndIf
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

yeah, okay, was thinking about this...

then you can forget the _API_DefWindowProc function at all, right ?

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

You don't need the line $id = ..., it gives the value of $GUI_RUNDEFMSG to $id. $GUI_RUNDEFMSG is better and I should have used that in the first place.

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If $hWnd = $child And $iMsg = $WM_NCHITTEST Then
        
        If $RUNDEFMSG = 2 Then;if it is the title handle
            $dragging = True
            Return 1;return the client handle
        Else
            $dragging = False
            Return $RUNDEFMSG;return the real handle
        EndIf
    Else
        Return $GUI_RUNDEFMSG 
    EndIf
EndFunc
The "If $RUNDEFMSG = 2" doesn't work for me...

I still think it's better off using $WM_NCLBUTTONDOWN = 0xA1 To register the msg..

Due to you can get the ID of the Titlebar control only when the left mouse button is down on it..

You can find if the mouse is down on the Titlebar, menu , min, max or close..

Also the function will only be called when when the left mouse is down on the titlebar.

To single out the click your after to register use the wParam as the id of what LMB Down on the titlebar

eg: Local $nID = BitAnd($wParam, 0x0000FFFF)

Titlebar Itself: $nID = 2

Titlebar Menu: $nID = 3

Titlebar Min button: $nID = 8

Titlebar Max button: $nID = 9

Titlebar Close button: $nID = 20

Cheers

Link to comment
Share on other sites

let's make that clear:

$WM_NCLBUTTONDOWN means mouse button down on menu bar ? (what is ncl supposed to mean ?)

but what for heaven's sake does $WM_NCHITTEST then mean ? (test? hittest? shittest ?) i really can't imagine.

so far martin's function works fine for my purpose with some modifications (pauses, an extra security exit out of the loop). i will give the nclbuttondown a try tomorrow because it seems to eat less cpu and you gave me the params to choose from.

thanks to you all

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

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...