Search code examples
vb6splash-screen

I want a .png Image as background of a form in vb6.0


I'm making a splash screen in vb6.0, I've managed to make the form transparent, But now I want to display a png image as the background picture of a form, So I'll get proper look of a splash screen.


Solution

  • "Splash screens" went out of style with Windows 3.1, and pretty much disappeared in serious software shortly afterward. But you can do this kind of thing with little trouble.

    It sounds like you want this splash screen to have "holes in it" where the PNG is transparent, for example perhaps a borderless Form you want to display as an irregular image. You'd need to render the PNG image on top of your backdrop chroma-key color.

    However since OLE/ActiveX has no transparent PNG rendering support VB doesn't offer a direct way to do this. Your options include things like the GDI+ Flat API or a GDI+ wrapper library such as WIA 2.0. WIA 2.0 has been part of Windows for a very long time now. It ships in Vista and later, and was once available as a redist library for Windows XP SP1 and later.

    Here's a brief example using WIA 2.0 which is fairly short to post. Note that it assumes the Project has a reference to Microsoft Windows Image Acquisition Library 2.0 set:

    Option Explicit
    
    Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongW" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongW" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long
    Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
        ByVal hWnd As Long, _
        ByVal crKey As Long, _
        ByVal bAlpha As Byte, _
        ByVal dwFlags As Long) As Long
    Private Const GWL_EXSTYLE = -20
    Private Const WS_EX_LAYERED = &H80000
    Private Const LWA_COLORKEY = &H1&
    
    Private Sub Form_Load()
        Dim W As Long
        Dim H As Long
        Dim ScanWidth As Long
        Dim Backdrop() As Byte
        Dim Y As Long
        Dim X As Long
        Dim BackImgF As WIA.ImageFile
    
        'Set the Form "transparent by color."
        SetWindowLong hWnd, _
                      GWL_EXSTYLE, _
                      GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
        SetLayeredWindowAttributes hWnd, RGB(0, 0, 1), 0, LWA_COLORKEY
    
        'Render PNG image into the Form with transparency.
        W = ScaleX(ScaleWidth, ScaleMode, vbPixels)
        H = ScaleY(ScaleHeight, ScaleMode, vbPixels)
        ScanWidth = ((3 * W + 3) \ 4) * 4
        ReDim Backdrop(ScanWidth * H - 1)
        For Y = 0 To H - 1
            For X = 0 To W - 1
                Backdrop(ScanWidth * Y + 3 * X) = 1 'RGB(0, 0, 1)
            Next
        Next
        With New WIA.Vector
            .BinaryData = Backdrop
            Set BackImgF = .ImageFile(W, H)
        End With
        With New WIA.ImageProcess
            .Filters.Add .FilterInfos!Stamp.FilterID
            With .Filters(1).Properties
                Set !ImageFile.Value = New WIA.ImageFile
                !ImageFile.Value.LoadFile "bg.png" 'Background PNG.
            End With
            Set Picture = .Apply(BackImgF).FileData.Picture
        End With
    End Sub
    

    If you want to load the PNG from a resource you can do that as well.

    If you must support Win2K or WinXP, or even WinXP SP1 or later but you do not have or do not want to deploy the redist WIA 2.0 library then you'll need a 3rd party GDI+ wrapper. Otherwise you could use the GDI+ Flat API calls. That is perfectly viable too but more work.