' EJERCICIO CON LAS PROPIEDADES DEL FORMULARIO
'OPACITY Y TRANSPARENCE.KEY - 30/10/2003
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Código generado por el Diseñador de Windows Forms "
Public Sub New()
MyBase.New()
'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()
'Agregar cualquier inicialización después de la llamada a InitializeComponent()
End Sub
'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Requerido por el Diseñador de Windows Forms
Private components As System.ComponentModel.IContainer
'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
'Puede modificarse utilizando el Diseñador de Windows Forms.
'No lo modifique con el editor de código.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btCerrar As System.Windows.Forms.Button
Friend WithEvents btHueco As System.Windows.Forms.Button
Friend WithEvents Tiempo As System.Windows.Forms.Timer
Friend WithEvents btHuecoNo As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Tiempo = New System.Windows.Forms.Timer(Me.components)
Me.Label1 = New System.Windows.Forms.Label
Me.btCerrar = New System.Windows.Forms.Button
Me.btHueco = New System.Windows.Forms.Button
Me.btHuecoNo = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
Me.Label1.Location = New System.Drawing.Point(24, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(344, 184)
Me.Label1.TabIndex = 0
Me.Label1.Visible = False
'
'btCerrar
'
Me.btCerrar.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.btCerrar.BackColor = System.Drawing.Color.Lavender
Me.btCerrar.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btCerrar.Location = New System.Drawing.Point(272, 232)
Me.btCerrar.Name = "btCerrar"
Me.btCerrar.Size = New System.Drawing.Size(96, 40)
Me.btCerrar.TabIndex = 2
Me.btCerrar.Text = "Cerrar formulario"
'
'btHueco
'
Me.btHueco.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.btHueco.BackColor = System.Drawing.Color.Lavender
Me.btHueco.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btHueco.Location = New System.Drawing.Point(24, 232)
Me.btHueco.Name = "btHueco"
Me.btHueco.Size = New System.Drawing.Size(96, 40)
Me.btHueco.TabIndex = 3
Me.btHueco.Text = "Abrir ventana"
'
'btHuecoNo
'
Me.btHuecoNo.Anchor = System.Windows.Forms.AnchorStyles.Bottom
Me.btHuecoNo.BackColor = System.Drawing.Color.Lavender
Me.btHuecoNo.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btHuecoNo.Location = New System.Drawing.Point(144, 232)
Me.btHuecoNo.Name = "btHuecoNo"
Me.btHuecoNo.Size = New System.Drawing.Size(104, 40)
Me.btHuecoNo.TabIndex = 4
Me.btHuecoNo.Text = "Cerrar Ventana"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Navy
Me.ClientSize = New System.Drawing.Size(392, 290)
Me.Controls.Add(Me.btHuecoNo)
Me.Controls.Add(Me.btHueco)
Me.Controls.Add(Me.btCerrar)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "Opacity y TransparencyKey"
Me.ResumeLayout(False)
End Sub
#End Region
'Al cargar el formulario
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Posición inicial del formulario
Me.Top = 150
Me.Left = 150
'sin botones de Max. / Min. / Cerrar
Me.ControlBox = False
End Sub
'Poner el mismo color a la etiqueta Label1 y a la propiedad
'TransparencyKey del formulario para que la etiqueta sea
'transparente. Solo funciona con la pantalla a 16 millones de colores o más
Private Sub btHueco_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btHueco.Click
'hacer visible la etiqueta que usaremos como ventana
Me.Label1.Visible = True
'asignar el mismo color a la etiqueta y a TransparencyKey
Me.Label1.BackColor = System.Drawing.Color.Gray
Me.TransparencyKey = Me.Label1.BackColor
End Sub
'Botón Cerrar para iniciar el evento Form1_Closing
Private Sub btCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCerrar.Click
Me.Label1.Visible = False
Me.Close()
End Sub
'Evento de cierre del formulario
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'mientras el valor de Opacity no llegue a cero
If Not (Me.Opacity = 0) Then
'cancelar el cierre del formulario
e.Cancel = True
'AddHandler (Instrucción): asocia un evento con un manipulador de eventos:
'se asocia el evento Trick del temporizador Tiempo con el procedimiento
'hacia el que apunta el operador AddressOf.
'AddressOf crea una instancia de delegado del procedimiento TickTiempo.
'Es decir, conecta el temporizador Tiempo con un manipulador
'de su evento Trick
AddHandler Tiempo.Tick, AddressOf TickTiempo
'iniciar el temporizador
Tiempo.Start()
End If
End Sub
'Manipulador del evento Trick del temporizador, se encarga de
'disminuir la opacidad del formulario cada período de tiempo
'especificado por la propiedad interval del temporizador
Private Sub TickTiempo(ByVal sender As System.Object, ByVal e As System.EventArgs)
Static i As Double = 1.0
i -= 0.08
Me.Opacity = i
'si el formulario es ya invisible
If Me.Opacity = 0 Then
'detener el temporizador
CType(sender, Timer).Stop()
'cerrar el formulario
Me.Close()
End If
End Sub
'ocultar la etiqueta transparente
Private Sub btHuecoNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btHuecoNo.Click
Me.Label1.Visible = False
End Sub
End Class