[Ex] Gui avec des contrôles déplacable
Posté : mar. 10 nov. 2009 05:34
par arrkhan
Voici un petit script permettant de modifier l'emplacement des contrôles de votre interface sans en perdre l'utilité.
une action est également affectées au label, qui est également déplacable.
une action est également affectées au label, qui est également déplacable.
► Afficher le texte
Code : Tout sélectionner
#include<Array.au3>
Opt("MouseCoordMode", 2)
Opt("guioneventmode", 1)
Global $cHoverID[2], $Global_cHID = 0, $cHoverActive = 0, $cHClicked = 0
$Gui = GUICreate("Drag Gui", 500, 200)
AdlibEnable("_CheckPressed", 10)
$DragLabel = GUICtrlCreateLabel("Dragable Label", 10, 10, 80, 15)
$Ok = GUICtrlCreateButton("OK", 10, 100, 100, 20)
$Cancel = GUICtrlCreateButton("Annuler", 10, 140, 100, 20)
$Lock = GUICtrlCreateButton("Débloquer les contrôles", 150, 10)
$FlagMove = 0
_ControlHover(2, $Gui, $DragLabel)
_ControlHover(2, $Gui, $Ok)
_ControlHover(2, $Gui, $Cancel)
_ControlHover(2, $Gui, $Lock)
GUISetState()
GUISetOnEvent(-3, "_Exit")
While 1
Sleep(10)
WEnd
Func _CheckPressed()
_ControlHover(0, $Gui)
$CtrlId2 = _ControlHover(1, $Gui)
$tempID2 = @extended
If $CtrlId2 = 1 Then
;ToolTip($tempID2, 0, 0, "DEBUG")
If $FlagMove = 1 Then
Switch $tempID2
Case $DragLabel
MoveControl(3)
Case $Ok
MoveControl(4)
Case $Cancel
MoveControl(5)
Case $Lock
GUICtrlSetData(6, "Débloquer les contrôles")
$FlagMove = 0
EndSwitch
Else
Switch $tempID2
Case $DragLabel
MsgBox(0, 'Test', 'Label', 2)
Case $Ok
MsgBox(0, 'Test', 'Ok', 2)
Case $Cancel
MsgBox(0, 'Test', 'Annuler', 2)
Case $Lock
GUICtrlSetData(6, "Bloquer les contrôles")
$FlagMove = 1
EndSwitch
EndIf
EndIf
EndFunc ;==>_CheckPressed
Func MoveControl($ID2Move)
Do
$Move = GUIGetCursorInfo()
$mousepos = MouseGetPos()
GUICtrlSetPos($ID2Move, $mousepos[0] - 40, $mousepos[1] - 10)
Sleep(10)
Until $Move[2] = 0
EndFunc ;==>MoveControl
Func _Exit()
AdlibDisable()
Exit
EndFunc ;==>_Exit
;===============================================================================
;
; Function Name: _ControlHover()
; Description: Enable Mouse/Hover Over Control Events
; Find if Mouse is over a known control
; Find if a Mouse has Clicked on a known control
; Add, Remove, or Verify the control within the known control list
; Parameter(s): $cH_Mode [default] 0 = Find if Mouse is over a known control
; [optional] 1 = Find if a Mouse has Clicked on a known control
; 2 = Add a control to the known control list
; 3 = Remove a control from the known control list
; 4 = Verify a control is in the known control list
; $cH_hWin [optional] - Window handle as returned by GUICreate()
; [preferred] - Window handle to save CPU usage and speed of _ControlHover()
; - and Avoid array errors for "non-active window"
; $cH_hCntrl [required] - Control ID to Add, Remove, or Verify the control within the known control list
; [not used] - to find if a mouse is over or has clicked on a known control
; Requirement(s): #include<Array.au3>
; Return Value(s):
; $cH_Mode 0 or 1
; On Success Return = 1 @error = 0, @extended = Current known control
; While Success Return = ""
; On Failure: Return = 0 @error = 1, @extended = Last known control
; $cH_Mode 2, 3, or 4
; On Success Return = 1 @error = 0, @extended = Function's return line number
; All $cH_Mode's
; On Error: Return = Description of the error
; @error = -1, @extended = Function's return line number
; Author(s): Valuater, Robert M
;
;===============================================================================
Func _ControlHover($cH_Mode = 0, $cH_hWin = "", $cH_hCntrl = "")
Local $cH_i, $cH_Data
If not @Compiled Then Opt("TrayIconDebug", 1) ; 1=debug line info
Select
Case $cH_Mode = 0 Or $cH_Mode = 1 ; 0 = Check if mouse is over a control, 1 = Check if mouse clicked on a control
; Developer did not add any controls before the function mode 0 or 1 was called
If UBound($cHoverID) = 2 Then ConsoleWrite("*** _ControlHover() Error *** " & @CRLF & "No Controls were Added" & @CRLF)
If $cH_hWin = "" Then
$cH_Data = GUIGetCursorInfo() ; Get cursor data from the active window
Else
If Not WinActive($cH_hWin) Then Return SetError( -1, 1, "Window not active") ; Dont waist CPU if the known window is not active
$cH_Data = GUIGetCursorInfo($cH_hWin) ; Get cursor data from the known window
EndIf
If Not IsArray($cH_Data) Then Return SetError( -1, 2, "Window not found") ; A readable GUI window was not found
For $cH_i = 1 to UBound($cHoverID) -1 ; Search the known controls for the currently hovered control
If $cH_Data[4] = $cHoverID[$cH_i] Then ; Mouse is over a current known control
; Mode 1 - check for a Click on a known control
If $cH_Mode And $cH_Data[2] = 1 and Not $cHClicked Then
$cHClicked = 1
Return SetError( 0, $cHoverID[$Global_cHID], 1) ; Mouse clicked on current control
ElseIf $cH_Mode And $cH_Data[2] <> 1 And $cHClicked Then
$cHClicked = 0
Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
EndIf
If $cH_Mode Then Return SetError ( "", "", "") ; Mouse is still over last known control and may click again
; Mode 0 & 1
If $cHoverID[$cH_i] = $cHoverID[$Global_cHID] And $cHoverActive Then Return SetError ( "", "", "") ; Mouse is still over last known control
If $cHoverActive Then
$cHoverActive = 0
Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new known control
EndIf
$cHoverActive = 1
$cHoverID[$Global_cHID] = $cHoverID[$cH_i] ; Re-set the current control to the new known control
Return SetError(0, $cHoverID[$Global_cHID], 1) ; Mouse is over the new-current known control
EndIf
Next
; hover control not found in the known control list
If $cHoverActive Then
If $cH_Mode And $cHClicked Then ; check - Release clicked current control
$cHClicked = 0
Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
EndIf
If $cH_Mode Then Return SetError ( "", "", "") ; Protect the release of the active control
$cHoverActive = 0
Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new unknown control
EndIf
If $cH_Mode And $cHClicked Then
$cHClicked = 0
Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
EndIf
Return SetError ( "", "", "")
Case $cH_Mode = 2 ; Add the new control to the known control list
If $cH_hCntrl = "" Then Return SetError( -1, 8, "Control not provided #1") ; The control ID was not given
_ArrayAdd($cHoverID, $cH_hCntrl) ; Add the control to the known control array
If @error Then Return SetError( -1, 9, "Control not added") ; Control not added to the known control list
Return SetError( 0, 10, 1) ; ; Control was added to the known control list
Case $cH_Mode = 3 ; Remove the control from the known control list
If $cH_hCntrl = "" Then Return SetError( -1, 11, "Control not provided #2") ; The control ID was not given
_ArrayDelete($cHoverID, $cH_hCntrl) ; Delete the control from the known control array list
If @error Then Return SetError( -1, 12, "Control not removed") ; Control not deleted from the known control list
Return SetError( 0, 13, 1) ; Control was deleted from the known control list
Case $cH_Mode = 4 ; Verify if control is in the known control list
If $cH_hCntrl = "" Then Return SetError( -1, 14, "Control not provided #3") ; The control ID was not given
_ArraySearch($cHoverID, $cH_hCntrl) ; Search to verify if control is in the known control list
If @error Then Return SetError( -1, 15, "Control not found") ; Control was not found in the known control list
Return SetError( 0, 16, 1) ; Control was found in the known control list
Case Else
Return SetError( -1, 17, "$cH_Mode incorrect") ; $cH_Mode has an incorrect value
EndSelect
Return SetError( -1, 18, "Unknown Error") ; Error is Unknown
EndFunc