Código:
' Gambas class file
Const VENTANA_ANCHO As Integer = 800
Const VENTANA_ALTO As Integer = 600
Private canvas As DrawingArea
Private timer1 As Timer
Private terminar As Boolean
Private pulsado As Boolean
Private cuenta As Integer
Private vigilante As Observer
Public Sub Form_Open()
vigilante = New Observer(Me) As "Vigilante"
With Me
.Background = Color.Black
.Title = "Evento MouseDown y MouseUP"
.Arrangement = Arrange.Horizontal
.Resizable = False
.AutoResize = True
End With
canvas = New DrawingArea(Me) As "Canvas"
With canvas
.W = VENTANA_ANCHO
.H = VENTANA_ALTO
.Background = Color.Black
End With
timer1 = New Timer As "Timer1"
Timer1.Delay = 1000
End
Public Sub Canvas_MouseDown()
If Not terminar Then
If Mouse.Left Then
pulsado = True
Timer1.Start
Endif
Endif
End
Public Sub Canvas_MouseUp()
If Not terminar Then
pulsado = False
cuenta = 0
Timer1.Stop
Endif
canvas.Refresh
End
Public Sub Canvas_Draw()
Dibujar
End
Public Sub Dibujar()
Dim RC As RectF
Dim mensaje As String
Paint.Font = Font["MonoSpace, 14"]
If pulsado And Not terminar Then
mensaje = "Se ha pulsado el boton izquierdo del ratón durante: " & Str(cuenta) & " segundos"
RC = Paint.TextSize(mensaje)
Else If Not pulsado And Not terminar Then
mensaje = "Se ha levantado el dedo del botón izquierdo del ratón"
RC = Paint.TextSize(mensaje)
Else If Not pulsado And terminar Then
mensaje = "Este programa se cerrará en " & Str(cuenta) & " segundos"
RC = Paint.TextSize(mensaje)
Endif
Paint.Text(mensaje, (canvas.w - RC.Width) / 2, (canvas.Height - RC.Height) / 2, RC.Width, RC.Height)
Paint.Brush = Paint.Color(Color.Green)
Paint.Fill
End
Public Sub Timer1_Timer()
If Not terminar Then
cuenta += 1
canvas.Refresh
Else If terminar And cuenta > 0 Then
cuenta -= 1
canvas.Refresh
Else If terminar And cuenta = 0 Then
Timer1.Stop
Me.Close
Endif
End
Public Sub Vigilante_Close()
If Not terminar Then
pulsado = False
terminar = True
cuenta = 4
Timer1.Start
Endif
End
Public Sub Form_Close()
If cuenta = 0 And terminar Then
Quit
Else
'Evitar el cierre del programa si no se cumplen las condiciones
Stop Event
Endif
End