Jump to content
Sign in to follow this  
Szhlopp

DragAndResize

Recommended Posts

Szhlopp

I've seen several posts for something like this so I thought I would write up a UDF for it :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#Include <Misc.au3>
#include <StaticConstants.au3>

;
Global $DragAndMove = False ; Has to be globally declared. Use this to enable/disable the function.


$hMainGUI = GUICreate("Drag and Resize", 625, 445, 193, 125)
$Edit1 = GUICtrlCreateEdit("", 59, 108, 132, 137)
GUICtrlSetData(-1, "Move me!")
$Edit2 = GUICtrlCreateEdit("", 224, 108, 382, 137)
GUICtrlSetData(-1, "Resize me!" & @CRLF & "In Size/Move mode, press 'Ctrl' before you click my sides")
$Label = GUICtrlCreateLabel("Test label. Move me!", 200, 300)
$SizeBox = GUICtrlCreateButton("Size/Move", 10, 410, 80, 30)
GUISetState(@SW_SHOW)


While 1
    Sleep(20)
    
    _DragAndResize($hMainGUI, $DragAndMove, $SizeBox)
    
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $SizeBox
            
            If $DragAndMove = False Then
                GUICtrlSetStyle($Edit2, $WS_SIZEBOX) ; Set the Edit controls to be re-sizeable
                GUICtrlSetStyle($Edit1, $WS_SIZEBOX) ; Set the Edit controls to be re-sizeable
                $DragAndMove = True
            ElseIf $DragAndMove = True Then
                GUICtrlSetStyle($Edit2, $GUI_SS_DEFAULT_EDIT) ; Set the Edit controls back to default
                GUICtrlSetStyle($Edit1, $GUI_SS_DEFAULT_EDIT) ; Set the Edit controls back to default
                $DragAndMove = False
            EndIf
            

    EndSwitch
WEnd

; The handle to the GUI
; Globally declared variable stating if this function is enabled or not
; Optional, if you have a button/control you DO NOT want to move/resize(In this case it's our button to enable/disable this function).
Func _DragAndResize($hMainGUI, ByRef $_DragEnabled, $Control = 999999)
    
    $aCI = GUIGetCursorInfo()
    If UBound($aCI) >=1 Then
        If $aCI[4] <> $hMainGUI And $_DragEnabled = True And $aCI[4] <> $Control Then
            Opt("MouseCoordMode", 0)
            GUICtrlSetState($aCI[4], $GUI_DISABLE)
            $aCI_LastItem = $aCI[4]
            While $aCI[4] <> 0 And $aCI[4] = $aCI_LastItem
                $aCI = GUIGetCursorInfo()
                Sleep(20)
                If _IsPressed(1) Then 
                    $aCP = ControlGetPos($hMainGUI, "", $aCI_LastItem)
                    $aMP = MouseGetPos()
                    $OsetX = $aMP[0] - $aCP[0]
                    $OsetY = $aMP[1] - $aCP[1]
                    ;------------MouseTrapParams-----------
                    $aWinP = WinGetPos($hMainGUI, "")
                    $aWinClient = WinGetClientSize($hMainGUI, "")
                    $TrapOffsetX = (($aWinP[2] - $aWinClient[0]) / 2) 
                    $TrapOffsetY = (($aWinP[3] - $aWinClient[1]) * .9)
                    ;------------MouseTrapParams-----------
                    _MouseTrap($aWinP[0] + $TrapOffsetX + ($OsetX - $TrapOffsetX) , $aWinP[1] + $TrapOffsetY + ($OsetY - $TrapOffsetY), $aWinP[0] + $aWinP[2] - $TrapOffsetX - ($aCP[2] - $OsetX) - 2, ($aWinP[1] + $aWinP[3] - $TrapOffsetY * .1) - ($aCP[3] - ($OsetY - $TrapOffsetY + 1)))
                    While _IsPressed(1)
                        Sleep(20)
                        $aMP = MouseGetPos()
                        GUICtrlSetPos($aCI_LastItem, $aMP[0] - $OsetX, $aMP[1] - $OsetY)
                    WEnd
                    _MouseTrap()
                    
                EndIf
                
                If _IsPressed(11) Then 
                    GUICtrlSetState($aCI_LastItem, $GUI_ENABLE)
                    While _IsPressed(11)
                        Sleep(20)
                    WEnd
                    GUICtrlSetState($aCI_LastItem, $GUI_DISABLE)
                EndIf
            WEnd
            GUICtrlSetState($aCI_LastItem, $GUI_ENABLE)
            Opt("MouseCoordMode", 1)
        EndIf
    EndIf
    
    
EndFunc

Hope this helps someone!

Szh

Share this post


Link to post
Share on other sites
gseller

Cool!! That is Groovy! LOL Very nice example!

Share this post


Link to post
Share on other sites
Zachlr

I am very impressed with this UDF. I have used the example and implemented it into my own code, it is fantastic. However, somewhere along the line I must have missed something, because when I impliment the code into a script of my own, I can no longer resize controls, only move them. I don't know what is wrong, since I am not familiar with UBound() Please tell me what I am doing wrong.

;I have modified the Sample Controls script with part of the DragAndResize UDF.
;It is now designed to activate when a key is pressed, rather than when a button is clicked.

; AutoIt 3.0.103 example
; 17 Jan 2005 - CyberSlug
; This script shows manual positioning of all controls;
;   there are much better methods of positioning...
#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>
#include <Misc.au3>
; GUI
$GUI = GuiCreate("Sample GUI", 400, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)


; MENU
GuiCtrlCreateMenu("Menu&One")
GuiCtrlCreateMenu("Menu&Two")
GuiCtrlCreateMenu("MenuTh&ree")
GuiCtrlCreateMenu("Menu&Four")

; CONTEXT MENU
$contextMenu = GuiCtrlCreateContextMenu()
GuiCtrlCreateMenuItem("Context Menu", $contextMenu)
GuiCtrlCreateMenuItem("", $contextMenu) ;separator
GuiCtrlCreateMenuItem("&Properties", $contextMenu)

; PIC
GuiCtrlCreatePic("logo4.gif",0,0, 169,68)
GuiCtrlCreateLabel("Sample pic", 75, 1, 53, 15)
GuiCtrlSetColor(-1,0xffffff)


; AVI
GuiCtrlCreateAvi("sampleAVI.avi",0, 180, 10, 32, 32, $ACS_AUTOPLAY)
GuiCtrlCreateLabel("Sample avi", 170, 50)


; TAB
GuiCtrlCreateTab(240, 0, 150, 70)
GuiCtrlCreateTabItem("One")
GuiCtrlCreateLabel("Sample Tab with tabItems", 250, 40)
GuiCtrlCreateTabItem("Two")
GuiCtrlCreateTabItem("Three")
GuiCtrlCreateTabItem("")

; COMBO
GuiCtrlCreatecombo("Sample Combo", 250, 80, 120, 100)

; PROGRESS
GuiCtrlCreateProgress(60, 80, 150, 20)
GuiCtrlSetData(-1, 60)
GuiCtrlCreateLabel("Progress:", 5, 82)

; EDIT
GuiCtrlCreateEdit(@CRLF & "  Sample Edit Control", 10, 110, 150, 70)

; LIST
GuiCtrlCreateList("", 5, 190, 100, 90)
GuiCtrlSetData(-1, "a.Sample|b.List|c.Control|d.Here", "b.List")

; ICON
GuiCtrlCreateIcon("shell32.dll", 1, 175, 120)
GuiCtrlCreateLabel("Icon", 180, 160, 50, 20)

; LIST VIEW
$listView = GuiCtrlCreateListView("Sample|ListView|", 110, 190, 110, 80)
GuiCtrlCreateListViewItem("A|One", $listView)
GuiCtrlCreateListViewItem("B|Two", $listView)
GuiCtrlCreateListViewItem("C|Three", $listView)

; GROUP WITH RADIO BUTTONS
GuiCtrlCreateGroup("Sample Group", 230, 120)
GuiCtrlCreateRadio("Radio One", 250, 140, 80)
GuiCtrlSetState(-1, $GUI_CHECKED)
GuiCtrlCreateRadio("Radio Two", 250, 165, 80)
GUICtrlCreateGroup ("",-99,-99,1,1)  ;close group

; UPDOWN
GuiCtrlCreateLabel("UpDown", 350, 115)
GuiCtrlCreateInput("42", 350, 130, 40, 20)
GuiCtrlCreateUpDown(-1)

; LABEL
GuiCtrlCreateLabel("Green" & @CRLF & "Label", 350, 165, 40, 40)
GuiCtrlSetBkColor(-1, 0x00FF00)

; SLIDER
GuiCtrlCreateLabel("Slider:", 235, 215)
GuiCtrlCreateSlider(270, 210, 120, 30)
GuiCtrlSetData(-1, 30)

; INPUT
GuiCtrlCreateInput("Sample Input Box", 235, 255, 130, 20)

; DATE
GuiCtrlCreateDate("", 5, 280, 200, 20)
GuiCtrlCreateLabel("(Date control expands into a calendar)", 10, 305, 200, 20)

; BUTTON
GuiCtrlCreateButton("Sample Button", 10, 330, 100, 30)

; CHECKBOX
GuiCtrlCreateCheckbox("Checkbox", 130, 335, 80, 20)
GuiCtrlSetState(-1, $GUI_CHECKED)

; TREEVIEW ONE
$treeOne = GuiCtrlCreateTreeView(210, 290, 80, 80)
$treeItem = GuiCtrlCreateTreeViewItem("TreeView", $treeOne)
GuiCtrlCreateTreeViewItem("Item1", $treeItem)
GuiCtrlCreateTreeViewItem("Item2", $treeItem)
GuiCtrlCreateTreeViewItem("Foo", -1)
GuiCtrlSetState($treeItem, $GUI_EXPAND)

; TREEVIEW TWO
$treeTwo = GuiCtrlCreateTreeView(295, 290, 103, 80, $TVS_CHECKBOXES)
GuiCtrlCreateTreeViewItem("TreeView", $treeTwo)
GuiCtrlCreateTreeViewItem("With", $treeTwo)
GuiCtrlCreateTreeViewItem("tvs_checkboxes", $treeTwo)
GuiCtrlSetState(-1, $GUI_CHECKED)
GuiCtrlCreateTreeViewItem("Style", $treeTwo)


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
    If _IsPressed(10) Then
    $hMainGUI = $GUI
    $_DragEnabled = True
    $Control = 0
    
    $aCI = GUIGetCursorInfo()
    If UBound($aCI) >=1 Then
        If $aCI[4] <> $hMainGUI And $_DragEnabled = True And $aCI[4] <> $Control Then
            Opt("MouseCoordMode", 0)
            GUICtrlSetState($aCI[4], $GUI_DISABLE)
            $aCI_LastItem = $aCI[4]
            While $aCI[4] <> 0 And $aCI[4] = $aCI_LastItem
                $aCI = GUIGetCursorInfo()
                Sleep(20)
                If _IsPressed(1) Then
                    $aCP = ControlGetPos($hMainGUI, "", $aCI_LastItem)
                    $aMP = MouseGetPos()
                    $OsetX = $aMP[0] - $aCP[0]
                    $OsetY = $aMP[1] - $aCP[1]
                    ;------------MouseTrapParams-----------
                    $aWinP = WinGetPos($hMainGUI, "")
                    $aWinClient = WinGetClientSize($hMainGUI, "")
                    $TrapOffsetX = (($aWinP[2] - $aWinClient[0]) / 2)
                    $TrapOffsetY = (($aWinP[3] - $aWinClient[1]) * .9)
                    ;------------MouseTrapParams-----------
                    _MouseTrap($aWinP[0] + $TrapOffsetX + ($OsetX - $TrapOffsetX) , $aWinP[1] + $TrapOffsetY + ($OsetY - $TrapOffsetY), $aWinP[0] + $aWinP[2] - $TrapOffsetX - ($aCP[2] - $OsetX) - 2, ($aWinP[1] + $aWinP[3] - $TrapOffsetY * .1) - ($aCP[3] - ($OsetY - $TrapOffsetY + 1)))
                    While _IsPressed(1)
                        Sleep(20)
                        $aMP = MouseGetPos()
                        GUICtrlSetPos($aCI_LastItem, $aMP[0] - $OsetX, $aMP[1] - $OsetY)
                    WEnd
                    _MouseTrap()
                   
                EndIf
               
                If _IsPressed(11) Then
                    GUICtrlSetState($aCI_LastItem, $GUI_ENABLE)
                    While _IsPressed(11)
                        Sleep(20)
                    WEnd
                    GUICtrlSetState($aCI_LastItem, $GUI_DISABLE)
                EndIf
            WEnd
            GUICtrlSetState($aCI_LastItem, $GUI_ENABLE)
            Opt("MouseCoordMode", 1)
        EndIf
    EndIf
   
EndIf
WEnd

I look forward to a response! Thanks,

Zach

Share this post


Link to post
Share on other sites
martin

Your function _DragAndResize prevents resizing when I try it. If I comment out the function then I can resize.


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.

Share this post


Link to post
Share on other sites
Zachlr

Your function _DragAndResize prevents resizing when I try it. If I comment out the function then I can resize.

I'm not sure which function you mean. I didn't declare it as a UDF. Could you post the working code, please?

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  

×