[英]I want a .png Image as background of a form in vb6.0
我正在 vb6.0 中制作啟動畫面,我已經設法使表單透明,但是現在我想顯示一個 png 圖像作為表單的背景圖片,所以我將獲得正確的啟動畫面外觀.
“閃屏”在 Windows 3.1 中過時了,不久之后在嚴肅的軟件中幾乎消失了。 但是你可以毫不費力地做這種事情。
聽起來您希望此啟動畫面在 PNG 透明的地方有“洞”,例如,您可能希望將無邊框表單顯示為不規則圖像。 您需要在背景色度鍵顏色之上渲染 PNG 圖像。
然而,由於 OLE/ActiveX 沒有透明的 PNG 渲染支持,VB 沒有提供直接的方法來做到這一點。 您的選擇包括 GDI+ Flat API 或 GDI+ 包裝器庫(如 WIA 2.0)。 WIA 2.0 已經成為 Windows 的一部分很長時間了。 它在 Vista 和更高版本中提供,並且曾經作為 Windows XP SP1 和更高版本的 redist 庫提供。
這是一個使用 WIA 2.0 的簡短示例,發布時間很短。 請注意,它假定項目引用了Microsoft Windows Image Acquisition Library 2.0
集:
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 庫,那么您將需要一個 3rd 方 GDI+ 包裝器。 否則,您可以使用 GDI+ Flat API 調用。 這也是完全可行的,但需要更多的工作。
我多年來一直在使用 LaVolpe 的 AlphaImageControl,它支持 PNG/光柵圖像。 下載文件和示例應用程序時,如果您使用的是 Windows 7 及更高版本,則需要使用 regsvr32 手動注冊控件。 然后,您需要傳遞對組件列表中控件的引用,瞧,您有一個對 vb6 具有魔力的控件。 這些文件可以在 vbForums 上找到 - 這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.