0
我得到以下代碼來加載一個帶alpha通道的PNG到表單上,並重新調整表單以匹配alpha通道。Alpha透明窗口的形式
Public Function applyAlphaForm(ByVal f As Form, ByVal bitmap As Bitmap, Optional ByVal opacity As Byte = 255) As Boolean
f.FormBorderStyle = FormBorderStyle.None
Dim style As Long
style = Win32.GetWindowLong(f.Handle, Win32.GWL_EXSTYLE)
If Not (style And Win32.WS_EX_LAYERED = Win32.WS_EX_LAYERED) Then
style = style Or Win32.WS_EX_LAYERED
Win32.SetWindowLong(f.Handle, Win32.GWL_EXSTYLE, style)
End If
Return SetBitmap(f, bitmap, opacity)
End Function
Public Function SetBitmap(ByVal f As Form, ByVal bitmap As Bitmap, ByVal opacity As Byte) As Boolean
f.Height = bitmap.Height
f.Width = bitmap.Width
If bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then
f.BackgroundImage = bitmap
f.TransparencyKey = bitmap.GetPixel(0, 0)
Return True
End If
Dim screenDC As IntPtr = Win32.GetDC(IntPtr.Zero)
Dim memDC As IntPtr = Win32.CreateCompatibleDC(screenDC)
Dim hBitmap As IntPtr = IntPtr.Zero
Dim oldBitmap As IntPtr = IntPtr.Zero
Try
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)) 'grab a GDI handle from this GDI+ bitmap
oldBitmap = Win32.SelectObject(memDC, hBitmap)
Dim size As Win32.Size = New Win32.Size(bitmap.Width, bitmap.Height)
Dim pointSource As Win32.Point = New Win32.Point(0, 0)
Dim topPos As Win32.Point = New Win32.Point(f.Left, f.Top)
Dim blend As Win32.BLENDFUNCTION = New Win32.BLENDFUNCTION()
blend.BlendOp = Win32.AC_SRC_OVER
blend.BlendFlags = 0
blend.SourceConstantAlpha = opacity
blend.AlphaFormat = Win32.AC_SRC_ALPHA
Win32.UpdateLayeredWindow(f.Handle, screenDC, topPos, size, memDC, pointSource, 0, blend, Win32.ULW_ALPHA)
Catch ex As Exception
Finally
Win32.ReleaseDC(IntPtr.Zero, screenDC)
If hBitmap <> IntPtr.Zero Then
Win32.SelectObject(memDC, oldBitmap)
Win32.DeleteObject(hBitmap)
End If
Win32.DeleteDC(memDC)
End Try
Return True
End Function
尼斯和容易的,但如果我把一些控制(按鈕,文本框...)的形式,他們就會消失。我的客人UpdateLayeredWindow將畫在hDC的形式,所以我們不能看到它背後的任何東西。那麼如何在表單上繪製一些表單控件呢?我已經嘗試遍歷所有控件,並在調用api之前呈現給png位圖,但這將是靜態圖像。
爲什麼你不只是將位圖轉換爲32bpp並擺脫該代碼? –
糟糕!我發現了一個解決方案:http://stackoverflow.com/questions/2979125/how-to-make-the-form-into-fully-transparent-32bit-alpha :( –
@Hans帕桑特:我想要一個來自PNG(某些不透明區域)的半透明數據... –