Autoit en español
Aplicaciones con Autoit - Juan Antonio Villalpando
-- Tutorial de iniciación a Autoit --
Volver al índice del tutorial
____________________________
20.- Semáforo.
- Código obtenido de:
https://www.autoitscript.com/forum/topic/186302-changing-graphic-colors/
- Trata de un semáforo que cambia sus luces roja, amarilla y verde cada vez que pulsamos sobre él.
__________________________
- Código completo.
semaforo.au3 |
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
; ============ Get focus on traffic lights then press Esc to Exit ===================
; ============ Traffic lights can be dragged ===================
Global $iFactor = 14 ; <<----- Bigger the number, the bigger the traffic lights size - say between 0.5 and 14+
Local $iLeft = 20, $iTop = 20
Global $aLights[3], $iFlag = 1
Global $hWnd = GUICreate("", 28 * $iFactor, 70 * $iFactor, $iLeft, $iTop, $WS_POPUP + $WS_BORDER, $WS_EX_TOPMOST)
GUISetBkColor(0x808080, $hWnd)
For $i = 0 To 2
$aLights[$i] = GUICtrlCreateGraphic(2 * $iFactor, (2 + ($i * 20)) * $iFactor, 28 * $iFactor, 31 * $iFactor)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000, $i = 0 ? 0xFF0000 : 0x000000) ;== red
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 2 * $iFactor, 2 * $iFactor, 20 * $iFactor, 20 * $iFactor)
Next
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
ExitLoop
Case $GUI_EVENT_PRIMARYDOWN
_SendMessage($hWnd, $WM_SYSCOMMAND, 0xF012, 0)
Case $GUI_EVENT_PRIMARYUP
_Change()
EndSwitch
WEnd
Func _Change()
$iFlag = Mod($iFlag + 1, 3)
GUICtrlSetGraphic($aLights[0], $GUI_GR_COLOR, 0x000000, $iFlag = 1 ? 0xFF0000 : 0x000000) ;== Red else Black
GUICtrlSetGraphic($aLights[0], $GUI_GR_ELLIPSE, 2 * $iFactor, 2 * $iFactor, 20 * $iFactor, 20 * $iFactor)
GUICtrlSetGraphic($aLights[0], $GUI_GR_REFRESH)
GUICtrlSetGraphic($aLights[1], $GUI_GR_COLOR, 0x000000, ($iFlag = 0) ? 0xFFFF00 : 0x000000) ;== Yellow else Black
GUICtrlSetGraphic($aLights[1], $GUI_GR_ELLIPSE, 2 * $iFactor, 2 * $iFactor, 20 * $iFactor, 20 * $iFactor)
GUICtrlSetGraphic($aLights[1], $GUI_GR_REFRESH)
GUICtrlSetGraphic($aLights[2], $GUI_GR_COLOR, 0x000000, $iFlag = 2 ? 0x00FF00 : 0x000000) ;== Green else Black
GUICtrlSetGraphic($aLights[2], $GUI_GR_ELLIPSE, 2 * $iFactor, 2 * $iFactor, 20 * $iFactor, 20 * $iFactor)
GUICtrlSetGraphic($aLights[2], $GUI_GR_REFRESH)
EndFunc ;==>_Change
|
________________________________________________
- Comentarios.
- Observa que para que cambie el color se ha de refrescar: GUICtrlSetGraphic($aLights[2], $GUI_GR_REFRESH)
- Código de círculos:
$circulo1 = GUICtrlCreateGraphic(100, 40, 50, 10)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0, 0xecc6d9)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 8, 8, 40, 40)
$circulo2 = GUICtrlCreateGraphic(235, 40, 50, 10)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0, 0xecc6d9)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 8, 8, 40, 40)
__________________________
__________________________
__________________________
- Al pulsar un botón cambia de color.
- Código obtenido de:
https://www.autoitscript.com/forum/topic/154428-how-to-change-the-color-of-a-rectangle/
boton_color.au3 |
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
;START gUI
$GUI1 = GUICreate("Rectangle Example", 448, 240, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUIClose")
$border = GUICtrlCreateGraphic(55, 16, 336, 145)
GUICtrlSetGraphic($border, $GUI_GR_COLOR, 0x00000, 0xFF0000)
GUICtrlSetGraphic($border, $GUI_GR_RECT, 0, 0, 322, 126)
$Button1 = GUICtrlCreateButton("Green", 56, 192, 89, 33)
GUICtrlSetOnEvent(-1, "TurnGreen")
$Button2 = GUICtrlCreateButton("Blue", 176, 192, 89, 33)
GUICtrlSetOnEvent(-1, "TurnBlue")
$Button3 = GUICtrlCreateButton("Purple", 288, 192, 89, 33)
GUICtrlSetOnEvent(-1, "TurnPurple")
GUISetState(@SW_SHOW)
;END GUI
While 1
WEnd
Func GUIClose()
Exit
EndFunc ;==>GUIClose
Func TurnGreen()
GUICtrlSetGraphic($border, $GUI_GR_COLOR, 0x00000, 0x00FF00)
GUICtrlSetGraphic($border, $GUI_GR_RECT, 0, 0, 322, 126)
GUICtrlSetGraphic($border, $GUI_GR_REFRESH)
EndFunc ;==>TurnGreen
Func TurnBlue()
GUICtrlSetGraphic($border, $GUI_GR_COLOR, 0x00000, 0x0080FF)
GUICtrlSetGraphic($border, $GUI_GR_RECT, 0, 0, 322, 126)
GUICtrlSetGraphic($border, $GUI_GR_REFRESH)
EndFunc ;==>TurnBlue
Func TurnPurple()
GUICtrlSetGraphic($border, $GUI_GR_COLOR, 0x00000, 0x8000FF)
GUICtrlSetGraphic($border, $GUI_GR_RECT, 0, 0, 322, 126)
GUICtrlSetGraphic($border, $GUI_GR_REFRESH)
EndFunc ;==>TurnPurple
|
_________________
- Propuesta.
- C
|