Aller au contenu

Recommended Posts

Posté(e)

Salut all,

Je encore faire un tuto à l'arrache. :P
Donc je vais vous expliquer comment faire pour faire un splashScreen, c'était l'image de présentation ou de chargement que l'on peu voir parfois avec certains programmes.

je vais prendre comme exemple:
logos_red-30cdb6e.png
il y a un dégradé de transparence et donc si j'utilise "tranparencyColor" ça va juste me retirer la couleur pure... alors dans ce cas il faut rien mettre dans transparency et mette un code kikoolong, enfin un peu long :P

/!\Les contrôles (outils) ne sont pas visible après la manipulation !/!\

Le code à mettre après "Public Class [Votreforme]"

#Region "full_decla"
    Public Structure BLENDFUNCTION
        Public BlendOp As Byte
        Public BlendFlags As Byte
        Public SourceConstantAlpha As Byte
        Public AlphaFormat As Byte
    End Structure
    Public Const ULW_COLORKEY As Integer = 1
    Public Const ULW_ALPHA As Integer = 2
    Public Const ULW_OPAQUE As Integer = 4
    Public Const AC_SRC_OVER As Byte = 0
    Public Const AC_SRC_ALPHA As Byte = 1
    Public Const GWL_EXSTYLE = (-20)
    Public Declare Function UpdateLayeredWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point, ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point, ByVal crKey As Integer, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Integer) As Boolean
    Private Declare Auto Function SetWindowLong Lib "User32.Dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Private Declare Auto Function GetWindowLong Lib "User32.Dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public Declare Function GetDC Lib "user32.dll" (ByVal hWnd As IntPtr) As IntPtr
    Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    Public Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hDC As IntPtr) As IntPtr
    Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Boolean
    Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hDC As IntPtr, ByVal hObject As IntPtr) As IntPtr
    Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Boolean
    Public Sub SetBitmap(ByVal PNG As Bitmap, ByVal opacity As Byte)
        If Not (PNG.PixelFormat = Imaging.PixelFormat.Format32bppArgb) Then
            Throw New ApplicationException("The bitmap must be 32ppp with alpha-channel.")
        End If
        Dim screenDc As IntPtr = GetDC(IntPtr.Zero)
        Dim memDc As IntPtr = CreateCompatibleDC(screenDc)
        Dim hBitmap As IntPtr = IntPtr.Zero
        Dim oldBitmap As IntPtr = IntPtr.Zero
        Try
            hBitmap = PNG.GetHbitmap(Color.FromArgb(0))
            oldBitmap = SelectObject(memDc, hBitmap)
            Dim size As Size = New Size(PNG.Width, PNG.Height)
            Dim pointSource As Point = New Point(0, 0)
            Dim topPos As Point = New Point(Left, Top)
            Dim blend As BLENDFUNCTION = New BLENDFUNCTION
            blend.BlendOp = AC_SRC_OVER
            blend.BlendFlags = 0
            blend.SourceConstantAlpha = opacity
            blend.AlphaFormat = AC_SRC_ALPHA
            UpdateLayeredWindow(Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, ULW_ALPHA)
        Finally
            ReleaseDC(IntPtr.Zero, screenDc)
            If Not (hBitmap.Equals(IntPtr.Zero)) Then
                SelectObject(memDc, oldBitmap)
                DeleteObject(hBitmap)
            End If
            DeleteDC(memDc)
        End Try
    End Sub
#End Region

Puis dans Form.load:

Dim Param As Long
        Param = GetWindowLong(Me.Handle, GWL_EXSTYLE)
        Call SetWindowLong(Me.Handle, GWL_EXSTYLE, Param Or (524288))
        Call SetBitmap(Me.BackgroundImage, 255)

___________
Tuto part2, un petit bonus :P

Comment faire une animation et faire en sorte que le splash se ferme un jour xD

Il suffit de mettre un timer avec un interval de 1000ms (1sec.)
et d'ajouter 2variales de type byte que on va appeler "Anim" et "Loader" (pense bête: faire un tuto sur les variables)
Donc vous mettez le code suivant entre de sub (si on le met dedans la variable va toujours se redéclarer et remettre la valeur de base.

Dim anim As Byte = 0
Dim loader As Byte = 0

Puis dans le Timer.tick (double clique sur le timer pour le voir) et dedans on va mettre:

anim += 1
        If anim = 1 Then
            Me.BackgroundImage = My.Resources.trame1 'la première image de l'animation 
        ElseIf anim = 10 Then
            Me.BackgroundImage = My.Resources.trame2 ' la seconde
        ElseIf anim = 11 Then
            Me.BackgroundImage = My.Resources.trame2 ' la quatrième ou troisième je sais pu
            loading += 1
            anim = 0
        End If
        Dim Param As Long
        Param = GetWindowLong(Me.Handle, GWL_EXSTYLE)
        Call SetWindowLong(Me.Handle, GWL_EXSTYLE, Param Or (524288))
        Call SetBitmap(Me.BackgroundImage, 255)
        If loader = 3 Then
            ' Pensez à mettre la forme suivante à ouvrir et aller dans les préférences de votre projet et changer le style de fermeture par fermeture du dernier formulaire
            Me.Close()
        End If

Voilà maintenant vous savez faire des SplashScreen stylé :D
le code de base et pas de moi j'ai oublié la source mais je l'ai bcp édité de toute façon :P

Bonne programmation à tous!
Cordialement,
Mars073

  • Upvote 1

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...