xiaoy 发表于 2011-1-25 08:52:50

能否随意移动控件

能否拖住控件在窗口内随意移动?窗口不动!

smartzbs 发表于 2011-1-25 14:49:50

本帖最后由 smartzbs 于 2011-1-25 16:18 编辑

鼠标移到Button按钮上,按下鼠标左键不放拖动该按钮.#Include <Misc.au3>
#include <Array.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $Button_1, $msg, $aPos, $aTemp
    $Form = GUICreate("My GUI Button")

    $Button_1 = GUICtrlCreateButton("Button", 10, 30, 100, 30)
        $aButton_1 = ControlGetPos($Form, "", $Button_1)

    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
                If _Hover($Form, $aButton_1, $aButton_1, $aButton_1, $aButton_1) Then
                        While _IsPressed(1)
                                If Not IsArray($aPos) Then $aPos = MouseGetPos()
                                $aTemp = MouseGetPos()
                                If $aTemp <> $aPos Or $aTemp <> $aPos Then
                                        ControlMove($Form, "", $Button_1, $aButton_1+($aTemp-$aPos), $aButton_1+($aTemp-$aPos) )
                                EndIf
                        WEnd
                        $aButton_1 = ControlGetPos($Form, "", $Button_1)
                        $aPos = ""
                EndIf
      $msg = GUIGetMsg()
      Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button_1
      EndSelect
    WEnd
EndFunc   ;==>Example

Func _Hover($cH_hWin = "", $Pos_X = 0, $Pos_Y = 0, $Pos_W = 0, $Pos_H = 0)
    $cH_Data = GUIGetCursorInfo($cH_hWin)
    If Not IsArray($cH_Data) Then Return SetError(10086)
    If $cH_Data > $Pos_X And $cH_Data < $Pos_X + $Pos_W And $cH_Data > $Pos_Y And $cH_Data < $Pos_Y + $Pos_H Then
      Return 1
    Else
                Return 0
    EndIf
EndFunc

chenronting 发表于 2011-1-25 20:09:46

楼上很给力啊, 谢谢分享。

xiaoy 发表于 2011-1-26 13:10:31

回复 2# smartzbs

谢谢smartzbs,您的代码非常好,不过我想再问一下,基于事件模式(OnEvent)下,如何写?我真是太笨了,老写不好。

lsqyx528 发表于 2011-1-28 17:54:11

很强悍,顶一个。。

menfan1 发表于 2011-1-29 10:44:27

强悍啊学习啦。。呵呵

wsfda 发表于 2011-2-1 13:27:16

谢谢分享,把他应用到软件界面里不知如何
页: [1]
查看完整版本: 能否随意移动控件