Jump to content

Dragging a window


Recommended Posts

I'm using the "Drag" function to drag a window and get its coordinates after it's dropped. The problem is that after I restore the GUIOnEventMode it will not drag anymore... reading the script will help in understanding what I'm trying to achieve.

Why can't I drag the window in that 2 second time gap???

Thx in advance

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
$win_x = 100
$win_y = 100
$win = GUICreate('none',100,100,$win_x,$win_y,$WS_POPUP)
Opt('GUIOnEventMode',1) 
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,'Drag')
GUISetState()
$dll = DllOpen('user32.dll')
While 1
    Sleep(1000)
WEnd
GUIDelete($win)
DllClose($dll)

Func Drag() 
    Opt('GUIOnEventMode',0)
    ToolTip('',0,0)
    $mpos = MouseGetPos()
    $dragdistx = $mpos[0] - $win_x
    $dragdisty = $mpos[1] - $win_y
    Sleep(100)
    While _IsPressed('01', $dll)
        WinMove($win,'',MouseGetPos(0)-$dragdistx,MouseGetPos(1)-$dragdisty)
        Sleep(20)
    WEnd
    $win_x = MouseGetPos(0)-$dragdistx
    $win_y = MouseGetPos(1)-$dragdisty
    Opt('GUIOnEventMode',1)
    ToolTip('try dragging me now',$win_x,$win_y-20)
    Sleep(2000)
    ToolTip('',0,0)
EndFunc
Edited by madflame991
Link to comment
Share on other sites

if im correct there is a command that you can use to move the window without having to drag it. Ill look it up and edit my post for you

edit: I ran your script and now i see what your trying to do,

try this it works better your delay was too long

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
$win_x = 100
$win_y = 100
$win = GUICreate('none',100,100,$win_x,$win_y,$WS_POPUP)
Opt('GUIOnEventMode',1) 
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,'Drag')
GUISetState()
$dll = DllOpen('user32.dll')
While 1
    Sleep(100)
WEnd
GUIDelete($win)
DllClose($dll)

Func Drag() 
    Opt('GUIOnEventMode',0)
    ToolTip('',0,0)
    $mpos = MouseGetPos()
    $dragdistx = $mpos[0] - $win_x
    $dragdisty = $mpos[1] - $win_y
    Sleep(100)
    While _IsPressed('01', $dll)
        WinMove($win,'',MouseGetPos(0)-$dragdistx,MouseGetPos(1)-$dragdisty)
        Sleep(20)
    WEnd
    $win_x = MouseGetPos(0)-$dragdistx
    $win_y = MouseGetPos(1)-$dragdisty
    Opt('GUIOnEventMode',1)
    ToolTip('try dragging me now',$win_x,$win_y-20)
    Sleep(100)
    ToolTip('',0,0)
EndFunc
Edited by Zmaster

A great place to start Autoit 1-2-3

Link to comment
Share on other sites

I know there's the WS_EX_PARENTDRAG thingie BUT I can't use that because I need the coordinates after the window has been moved... the formula for getting the window dragged and retrieving it's coordinates after has been discussed here: http://www.autoitscript.com/forum/index.ph...mario&st=45 and I have no intention of changing it...

My problem is that I fail to understand why it cannot be dragged even is I set Guisetonevent back to 1

Edit:

I wanted it to be like 10 seconds... that's the problem... it should call the "Drag" function while it's "sleeping"

Edited by madflame991
Link to comment
Share on other sites

its because you have Sleep (2000) it creates a delay that your probably not waiting out. try and reduce the sleep to 100

so... isn't it supposed to call "Drag" again while it's sleeping 2 seconds? I restored GUISETONEVENT to 1 before the 2 sec sleep

Link to comment
Share on other sites

How about using WS_EX_PARENTDRAG to drag the window and then use MouseGetPos() when WM_ENTERSIZEMOVE and WM_EXITSIZEMOVE is sent to get your positions??

Edited by AdmiralAlkex
Link to comment
Share on other sites

If its called sometime during the 2 second sleep nothing is going to happen Sleep pauses the script exicution for the specified amount of time meaning during that 2 seconds nothing is going to happen at all so reduce your sleeps to 100

...the loop from the main program is just a sleep, and things like OnEvents thingies happen; HotKey stuff happends too...

As I explained earlier: my goal is to let the user see the tooltip for more than 100 ms (preferably 10 seconds); and during that time it would be nice if the window would be draggable

The window is draggable in the While 1 Sleep(1000) WEnd ... why isn't it draggable in that other Sleep from the end of the function

Edit:

@AdmiralAlkex

that's a new trick, I'll try it!

@GUIWSEXPARENTDRAG ... "Allow the label or pic control to be used as the title bar to drag the whole the parent window." I've got no label or pic... it's just a window... and I don't want to create and empty label cause that window is meant to be a "screenmate character floating on your desktop"

Edited by madflame991
Link to comment
Share on other sites

my fix is easier to do lol

How can it be easier when it uses double the amount of code and still doesn't do what madflame991 wants? You should just STFU unless you actually know what you are talking about.
Link to comment
Share on other sites

How can it be easier when it uses double the amount of code and still doesn't do what madflame991 wants? You should just STFU unless you actually know what you are talking about.

wow dude, it does what hes trying to do already he only needs to change 4 characters from what he already has it requires less work from where he is at now

A great place to start Autoit 1-2-3

Link to comment
Share on other sites

@Zmaster... sorry but, your fix is useless... cause it's chopping away the tooltip!

Let's be cool... and wait for other's advice too...

and thx for trying to help me

Link to comment
Share on other sites

The window is draggable in the While 1 Sleep(1000) WEnd ... why isn't it draggable in that other Sleep from the end of the function

Maybe you will understand if you think about it this way:

When using GUIOnEventMode only one event can be active at a time, so when you move your window while it is in Sleep(2000) the new event is added to a queue and is executed after the current event is finished. (this is seen with the code in your first post if you move the window around)

That means that if you want your script to do some lengthy operation that you want to be interruptable you should switch a flag when it's supposed to be started (for example a press on a button) like $DoSomething = 1 and then in the main-loop check for "If $DoSomething = 1 Then ...." , that way it can then be interrupted if some other event triggers.

Link to comment
Share on other sites

Maybe you will understand if you think about it this way:

When using GUIOnEventMode only one event can be active at a time, so when you move your window while it is in Sleep(2000) the new event is added to a queue and is executed after the current event is finished. (this is seen with the code in your first post if you move the window around)

That clears it, thanks! That's the kind of explanation I was hoping for :)

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