Jump to content
Sign in to follow this  
SBrown

Draging a window?

Recommended Posts

SBrown

How can i drag a gui by the gray background area? I want the user to be able to drag a popup around on their screen.

Share this post


Link to post
Share on other sites
picea892

Hi there

Three examples for you to chew on.

;Created by Zedna, modified by Siao
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode",1)

$gui = GuiCreate("Title",300,200,-1,-1,BitOR($WS_POPUP,$WS_BORDER,$WS_SIZEBOX))
GUISetOnEvent ($GUI_EVENT_CLOSE, "Quit" )
GUISetOnEvent ($GUI_EVENT_PRIMARYDOWN, "Drag" )
$a=GUICtrlCreateButton("Hi",100,100,50,50)
GUICtrlSetOnEvent(-1,"runner")
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Drag()
    $a = GUIGetCursorInfo($gui)
    If $a[4] = 0 Then ;or exclude specific controlIDs
        dllcall("user32.dll","int","ReleaseCapture")
        dllcall("user32.dll","int","SendMessage","hWnd", $gui,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
    EndIf
EndFunc

Func Quit()
    Exit
EndFunc

func runner()
MsgBox(0,"","You pressed hi")
EndFunc

; Move any Window - Click & Drag

; Author Valuater

Run("notepad.exe")
WinWaitActive("")

While WinExists("Untitled -")
    _WinMove("Untitled -")
    Sleep(10)
WEnd

;===============================================================================
;
; Description:      Moves any Window by Left Mouse "Click & Drag"
; Syntax:           _WinMove($hWnd) or  _WinMove($s_Title)
; Parameter(s):     $s_hWnd = as returned by GUICreate()
;                   $s_Title = title of window to be moved
; Requirement(s):   None
; Return Value(s):  On Success - Repositions the Window
;                   On Failure - Returns ""
; Author(s):        Valuater,  Valuater [at] aol [.com], Inspired by Martin
;
;===============================================================================
Func _WinMove($hWnd)
    If Not WinActive($hWnd) Then Return
    Local $a_R = DllCall('user32.dll', "int", "GetAsyncKeyState", "int", '0x1')
    If @error Or BitAND($a_R[0], 0x8000) <> 0x8000 Then Return
    Local $a = WinGetPos($hWnd), $b = MouseGetPos()
    If $b[0] < $a[0] Or $b[1] < $a[1] Or $b[0] > $a[2] + $a[0] Or $b[1] > $a[3] + $a[1] Then Return
    While WinActive($hWnd)
        Local $c = MouseGetPos()
        WinMove($hWnd, '', $a[0] + $c[0] - $b[0], $a[1] + $c[1] - $b[1])
        $a_R = DllCall('user32.dll', "int", "GetAsyncKeyState", "int", '0x1')
        If @error Or BitAND($a_R[0], 0x8000) <> 0x8000 Then Return
    WEnd
EndFunc   ;==>_WinMove

; press left and right mouse buttons
#Include <Misc.au3>
#Include <WinAPI.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
global $dll = DllOpen("user32.dll")

While 1
    sleep(100)
    $hWnd =WinGetHandle("","")

if _IsPressed("01", $dll) Then 
    Do
            
        sleep(100)
    until _IsPressed("02", $dll) or not _IsPressed("01", $dll)
    $state = WinGetState($hWnd)
if _IsPressed("02", $dll) and not BitAnd($state, 32) Then
            movewin($hWnd)
    ;       MouseClick("left")
EndIf
    
EndIf
WEnd


func movewin($hWnd)
Local $MousePos, $WinPos, $PosDiff[2]
    While 1
        $MousePos = MouseGetPos ()
        $WinPos = WinGetPos ("","")
        $PosDiff[0] = $WinPos[0] - $MousePos[0]
        $PosDiff[1] = $WinPos[1] - $MousePos[1]
        While _IsPressed ("01", $dll)
            $MousePos = MouseGetPos ()
            WinMove ("", "", $MousePos[0] + $PosDiff[0], $MousePos[1] + $PosDiff[1])
            $WinPos = WinGetPos ("","")
            Sleep (10)
        WEnd
        ExitLoop
        Sleep (10)
    WEnd 
EndFunc
Edited by picea892

Share this post


Link to post
Share on other sites
SBrown

thanks a bunch

Share this post


Link to post
Share on other sites
rdwray

Can this code be made to work with the right mouse button? I have tried and failed. Thanks...

;Created by Zedna, modified by Siao
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode",1)

$gui = GuiCreate("Title",300,200,-1,-1,BitOR($WS_POPUP,$WS_BORDER,$WS_SIZEBOX))
GUISetOnEvent ($GUI_EVENT_CLOSE, "Quit" )
GUISetOnEvent ($GUI_EVENT_PRIMARYDOWN, "Drag" )
$a=GUICtrlCreateButton("Hi",100,100,50,50)
GUICtrlSetOnEvent(-1,"runner")
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Drag()
    $a = GUIGetCursorInfo($gui)
    If $a[4] = 0 Then ;or exclude specific controlIDs
        dllcall("user32.dll","int","ReleaseCapture")
        dllcall("user32.dll","int","SendMessage","hWnd", $gui,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
    EndIf
EndFunc

Func Quit()
    Exit
EndFunc

func runner()
MsgBox(0,"","You pressed hi")
EndFunc


“No other God have I but Thee; born in a manger, died on a tree.” Martin Luther

Share this post


Link to post
Share on other sites
Melba23

rdwray,

Could I suggest a read of the Moving and Resizing PopUp GUIs tutorial in the Wiki? Lots of good tips on how to do it there! :x

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
rdwray

rdwray,

Could I suggest a read of the Moving and Resizing PopUp GUIs tutorial in the Wiki? Lots of good tips on how to do it there! :x

M23

The very first piece of code I found on Wiki is strange. Normally if a control is placed on top of another one, both control retain their attributes, but here, the button becomes part of the label. How and why? If the button becomes a label, it loses its' value as an independent control. Thanks...
#include <GuiconstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "On_Exit")

$hGUI = GUICreate("X", 100, 100, -1, -1, $WS_POPUP)
GUISetBkColor(0xFF0000, $hGUI)
$hLabel = GUICtrlCreateLabel("", 0, 0, 100, 50, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetBkColor(-1, 0x00FF00)
;GUICtrlSetState(-1, $GUI_DISABLE)
$hButton = GUICtrlCreateButton("Test", 10, 35, 80, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $hButton
            On_Button()
        Case $hLabel
            On_Exit()
        Case $hGUI
            MsgBox(0, "Hi", "GUI clicked")
    EndSwitch
WEnd

Func On_Button()
    MsgBox(0, "Hi", "Button Pressed")
EndFunc   ;==>On_Button

Func On_Exit()
    Exit
EndFunc   ;==>On_Exit

“No other God have I but Thee; born in a manger, died on a tree.” Martin Luther

Share this post


Link to post
Share on other sites
Melba23

rdwray,

Normally if a control is placed on top of another one, both control retain their attributes

Not always so. If 2 active controls overlap, normally neither can be actioned - which is why background Pic controls need to be disabled. :shifty:

However, a label is actioned on mouse button down, while a button is actioned on mouse button up. So in this particular case, the label will be actioned on mouse button down and the mouse button up is swallowed by the system as indicating the end of the label action rather than acting as the button activation. :x

I hope that makes sense - please ask if not. :P

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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  

×