lunedì 31 ottobre 2011

[VB.NET] Forms con Effetto Dissolvenza

Descrizione :
Tecnica per ottenere un effetto Fade-In / Fade-Out da associare all'apertura / chiusura Form.

+ Articolo :

Contrariamente a quanto alcuni pensano, la Proprietà Opacity non è di tipo intero e il suo valore non va da 0 a 100.
Nella pratica gli unici valori realmente utili vanno da 0 a 1.
Opacity è di tipo Double, e l'assegnazione di un qualsiasi valore Double al di fuori del range non produce errori.
Con Opacity = 0 si ha la totale trasparenza, mentre con Opacity = 1, la totale opacità.

Ogni tentativo di passare un valore al di fuori del Range [0>1] viene tollerato, ma l'effetto sarà ininfluente :

       Me.Opacity = 1.1
       MessageBox.Show(Me.Opacity)

       Me.Opacity = -0.1
       MessageBox.Show(Me.Opacity)

--> Form1 :

1. Contiene un Timer "tmr_fading". L'Interval deve essere molto contenuto, ad esempio 10 o 20 msec.

2. La Form interessata deve avere a design la proprietà Opacity già impostata a 0%.

--> Codice :
Public Class Form1

    Private m_op As Double = 0
    Private m_close As Boolean = False
    Private m_fadeinout As Boolean ' IN=True / OUT=False

    Private Sub LoadFadeIn()
        m_fadeinout = True
        tmr_fading.Enabled = True
    End Sub

    Private Sub CloseFadeOut()
        m_fadeinout = False
        tmr_fading.Enabled = True
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFadeIn()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = Not m_close
        CloseFadeOut()
    End Sub

    Private Sub tmr_fading_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_fading.Tick
        If m_fadeinout = True Then
            m_op += 1
        Else
            m_op -= 1
        End If
        Me.Opacity = m_op / 100
        If m_op >= 100 Then DirectCast(sender, Timer).Enabled = False
        If m_op <= 0 Then
            m_close = True
            Me.Close()
        End If
    End Sub

End Class

+ Fine Articolo.

Un Click su "Mi Piace" è il modo migliore per ringraziare l'autore di questo articolo.



0 commenti:

Posta un commento

Favorites Twitter Facebook Delicious Digg Stumbleupon More

 
Design by Free WordPress Themes Modificato da MarcoGG