萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

VB.NET 設定視窗透明度完整實作

核心 API:Layered Window 屬性

Windows 2000+ 支援分層視窗 (WS_EX_LAYERED),透過 SetLayeredWindowAttributes 控制整體透明度或色彩鍵透明。

模組程式碼 (Module: modTransparency)

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

.NET Framework / VB.NET 2002+ 內建屬性 (更簡單)

' 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)

進階:逐像素 Alpha (Per-Pixel Alpha) - 非矩形視窗

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
' 可實現圓角視窗、不規則形狀、陰影、漸層透明等效果

注意事項

  • 效能:分層視窗會增加 GPU/CPU 合成負擔,避免在大量視窗或低階硬體上濫用。
  • 子控制項:SetLayeredWindowAttributes 的 Alpha 作用於整個視窗 (含子控制項)。若需子控制項不透明,必須用 UpdateLayeredWindow 配合 32bpp 位圖。
  • 顯示卡驅動:舊驅動可能不支援硬體加速合成,導致閃爍或高 CPU。
  • Vista/Win7+ DWM:桌面視窗管理器已內建合成,WS_EX_LAYERED 效能較佳。
copyright © 萬盛學電腦網 all rights reserved