Windows 2000+ 支援分層視窗 (WS_EX_LAYERED),透過 SetLayeredWindowAttributes 控制整體透明度或色彩鍵透明。
Option Explicit
' API 宣告
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(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_ALPHA = 2
Private Const LWA_COLORKEY = 1
' ============================================================
' 設定表單整體透明度 (0-255, 0=全透明, 255=不透明)
' ============================================================
Public Sub SetFormOpacity(frm As Form, ByVal alpha As Byte)
Dim lngExStyle As Long
' 1. 取得目前擴充風格
lngExStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
' 2. 加入 WS_EX_LAYERED
If (lngExStyle And WS_EX_LAYERED) = 0 Then
lngExStyle = lngExStyle Or WS_EX_LAYERED
SetWindowLong frm.hwnd, GWL_EXSTYLE, lngExStyle
End If
' 3. 設定 Alpha
SetLayeredWindowAttributes frm.hwnd, 0, alpha, LWA_ALPHA
End Sub
' ============================================================
' 設定色彩鍵透明 (指定顏色變透明)
' ============================================================
Public Sub SetFormTransparencyColorKey(frm As Form, ByVal colorKey As Long)
Dim lngExStyle As Long
lngExStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
If (lngExStyle And WS_EX_LAYERED) = 0 Then
lngExStyle = lngExStyle Or WS_EX_LAYERED
SetWindowLong frm.hwnd, GWL_EXSTYLE, lngExStyle
End If
SetLayeredWindowAttributes frm.hwnd, colorKey, 0, LWA_COLORKEY
End Sub
' ============================================================
' 移除分層屬性 (恢復正常)
' ============================================================
Public Sub RemoveLayeredStyle(frm As Form)
Dim lngExStyle As Long
lngExStyle = GetWindowLong(frm.hwnd, GWL_EXSTYLE)
If (lngExStyle And WS_EX_LAYERED) <> 0 Then
lngExStyle = lngExStyle And (Not WS_EX_LAYERED)
SetWindowLong frm.hwnd, GWL_EXSTYLE, lngExStyle
End If
End Sub
Private Sub Form_Load()
' 設定 70% 不透明度 (alpha = 179)
SetFormOpacity Me, 179
' 或設定特定顏色透明 (例如背景色 RGB(255,0,255) 變透明)
' SetFormTransparencyColorKey Me, RGB(255, 0, 255)
End Sub
Private Sub TrackBar_Scroll()
' 滑桿 0-255 即時調整
SetFormOpacity Me, CByte(TrackBar.Value)
lblOpacity.Caption = "透明度: " & TrackBar.Value
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 釋放資源
RemoveLayeredStyle Me
End Sub
' Windows Forms 內建 Opacity 屬性 (0.0 - 1.0)
Me.Opacity = 0.7 ' 70%
' WPF
Window.Opacity = 0.7
' 注意:Opacity 作用於整個視窗包含子控制項
' 若只想讓背景透明而控制項不透明,需用 WPF 的 AllowsTransparency=True + WindowStyle=None
' 或 Win32 API 色彩鍵/逐像素 Alpha (UpdateLayeredWindow)
Private Declare Function UpdateLayeredWindow Lib "user32" _
(ByVal hwnd As Long, ByVal hdcDst As Long, ByRef pptDst As POINTAPI, _
ByRef psize As SIZE, ByVal hdcSrc As Long, ByRef pptSrc As POINTAPI, _
ByVal crKey As Long, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Long) As Long
Private Type POINTAPI: x As Long: y As Long: End Type
Private Type SIZE: cx As Long: cy As Long: End Type
Private Type BLENDFUNCTION
BlendOp As Byte
BlendFlags As Byte
SourceConstantAlpha As Byte
AlphaFormat As Byte
End Type
Private Const ULW_ALPHA = 2
Private Const AC_SRC_OVER = 0
Private Const AC_SRC_ALPHA = 1
' 使用 32bpp PARGB Bitmap 繪製任意形狀,再 UpdateLayeredWindow
' 可實現圓角視窗、不規則形狀、陰影、漸層透明等效果
SetLayeredWindowAttributes 的 Alpha 作用於整個視窗 (含子控制項)。若需子控制項不透明,必須用 UpdateLayeredWindow 配合 32bpp 位圖。WS_EX_LAYERED 效能較佳。