萬盛學電腦網

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

VB.NET 表單特效:浮動視窗與透明度動態控制

專案類型

Windows Forms (.NET Framework / .NET Core 3.1+ / .NET 5+)

特效一:浮動視窗 (自動來回移動)

設計邏輯

  • 兩個 `Timer` 交替啟用:`Timer1` 向右下移動,`Timer2` 向左上移動。
  • 邊界檢查:超過設定範圍切換 Timer。
  • `DesktopLocation` 直接設定螢幕座標 (不受父容器影響)。

程式碼 (VB.NET)

Public Class FloatingForm
    Private ReadOnly _bounds As New Rectangle(100, 50, 500, 350) ' 允許移動區域 X,Y,Width,Height

    Private Sub FloatingForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DesktopLocation = _bounds.Location
        Timer1.Interval = 10   ' 10ms ≈ 100 FPS
        Timer2.Interval = 10
        Timer1.Enabled = True
        Timer2.Enabled = False
        Me.FormBorderStyle = FormBorderStyle.None  ' 無邊框更像浮動小工具
        Me.BackColor = Color.Black
        Me.TransparencyKey = Color.Black  ' 黑色透明 (配合背景圖)
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim nextLoc = New Point(Me.DesktopLocation.X + 2, Me.DesktopLocation.Y + 1)
        If nextLoc.X <= _bounds.Right - Me.Width AndAlso nextLoc.Y <= _bounds.Bottom - Me.Height Then
            Me.DesktopLocation = nextLoc
        Else
            Timer1.Enabled = False
            Timer2.Enabled = True
        End If
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Dim nextLoc = New Point(Me.DesktopLocation.X - 2, Me.DesktopLocation.Y - 1)
        If nextLoc.X >= _bounds.Left AndAlso nextLoc.Y >= _bounds.Top Then
            Me.DesktopLocation = nextLoc
        Else
            Timer1.Enabled = True
            Timer2.Enabled = False
        End If
    End Sub
End Class

特效二:透明度動態調整 (TrackBar + Timer 輪詢)

介面配置

  • `TrackBar1`:`Minimum=0`, `Maximum=100`, `Value=100`, `TickFrequency=10`
  • `Label1`:Text="透明度: 100%"
  • `Timer1`:`Interval=100`, `Enabled=True` (輪詢 TrackBar 值套用至表單)

程式碼

Public Class TransparentForm
    Private Sub TransparentForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.FixedSingle
        Me.AllowTransparency = True  ' 關鍵:允許表單透明
        TrackBar1.Value = 100
        UpdateOpacityLabel()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' 0~100 → 0.0~1.0
        Me.Opacity = TrackBar1.Value / 100.0
        UpdateOpacityLabel()
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        UpdateOpacityLabel()
    End Sub

    Private Sub UpdateOpacityLabel()
        Label1.Text = $"透明度: {TrackBar1.Value}% (Opactiy={Me.Opacity:F2})"
    End Sub
End Class

關鍵屬性對照表

屬性/方法 用途 注意事項
Form.Opacity 整體透明度 (0.0~1.0) 需 `AllowTransparency=True`;WPF 用 `Window.Opacity`
Form.TransparencyKey 指定顏色完全透明 (點擊穿透) 配合 `BackColor` 使用;點擊透明區會穿透到下層視窗
Form.FormBorderStyle = None 無邊框/標題列 需自行實作拖曳移動 (`MouseDown` + `ReleaseCapture`/`SendMessage`)
Form.TopMost = True 永遠置頂 工具列/通知類應用常用
Form.ShowInTaskbar = False 不在工作列顯示 搭配 NotifyIcon 使用

拖曳無邊框視窗通用代碼

' VB.NET
Private Sub Form_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    If e.Button = MouseButtons.Left Then
        ReleaseCapture()
        SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    End If
End Sub

Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Private Const HTCAPTION As Integer = 2
Private Declare Function ReleaseCapture Lib "user32.dll" () As Boolean
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

效能與相容性提醒

  • `Opacity` < 1.0 會啟用 **分層視窗 (WS_EX_LAYERED)**,可能導致:
    • GDI+ 繪圖效能下降 (建議啟用 `DoubleBuffered = True`)。
    • 某些顯卡驅動程式閃爍/渲染異常。
    • 遠端桌面 (RDP) 環境下透明度可能失效或變黑。
  • 高 DPI (Per-Monitor DPI Awareness) 下座標計算需使用邏輯像素,或設定 `AutoScaleMode = Dpi`。
  • Linux (WinForms on .NET 6+) 對 `Opacity`/`TransparencyKey` 支援有限,視 Window Manager (Wayland/X11) 而定。

結語

浮動視窗與透明度控制是桌面應用打造「小工具感」體驗的關鍵技巧。善用 `Timer` + `DesktopLocation` 實現平滑移動,`TrackBar` + `Opacity` 即時調整透明度,並注意 `AllowTransparency`、`FormBorderStyle`、`TopMost` 等屬性組合。現代專案若跨平台需求強,建議評估 **Avalonia UI**、**Uno Platform** 或 **Blazor Hybrid** 等原生支援分層透明的框架。

copyright © 萬盛學電腦網 all rights reserved