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