我想要一个 .png 图像作为 vb6.0 中表单的背景

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

我在vb6.0中制作启动画面,我已经设法使窗体透明,但现在我想显示一个png图像作为窗体的背景图片,所以我会得到正确的启动画面的外观。

多年来我一直在使用 LaVolpe 的 AlphaImageControl,它支持 PNG/raster 图像。下载文件和示例应用程序时,如果您使用的是 Windows 7 及更高版本,则需要使用 regsvr32 手动注册控件。然后,您需要在组件列表中传递对控件的引用,瞧,您拥有了一个对 vb6 具有魔力的控件。这些文件可以在 vbForums 找到 - HERE

"Splash screens" 在 Windows 3.1 中过时了,不久之后在正式软件中几乎消失了。但是你可以毫不费力地做这种事情。

听起来您希望启动画面具有 "holes in it",其中 PNG 是透明的,例如您希望显示为不规则图像的无边框表单。您需要在背景色键颜色之上渲染 PNG 图像。

但是由于 OLE/ActiveX 没有透明 PNG 渲染支持 VB 没有提供直接的方法来做到这一点。您的选择包括 GDI+ Flat API 或 WIA 2.0 等 GDI+ 包装器库。 WIA 2.0 已经成为 Windows 的一部分很长时间了。它在 Vista 及更高版本中发布,并且曾经作为 Windows XP SP1 及更高版本的 redist 库提供。

这是一个使用 WIA 2.0 的简短示例,它比 post 还短。请注意,它假定项目引用了 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

如果您想从资源加载 PNG,您也可以这样做。

如果您必须支持 Win2K 或 WinXP,甚至 WinXP SP1 或更高版本,但您没有或不想部署 redist WIA 2.0 库,那么您将需要第 3 方 GDI+ 包装器。否则,您可以使用 GDI+ Flat API 调用。这也完全可行,但需要更多工作。