[VB.NET]透明背景的可拖曳無框表單

[VB.NET]透明背景的可拖曳無框表單

我先在http://www.iconfinder.com/ 隨便找一張png透明圖片

然後將圖片載入資源檔,建置動作設為 內嵌資源

把圖片背景透明化主要是利用下面兩行

Me.BackColor = System.Drawing.SystemColors.Control
Me.TransparencyKey = System.Drawing.SystemColors.Control

其他的code則是無框拖曳用到的事件

完成後的結果就是一個透明背景的可拖曳無框表單

image

 

完整的code如下

 


Public Class Form1
    Private Check As Integer
    Private temp_x As Integer
    Private temp_y As Integer
    Private pointTemp As Point
    Public Sub New()
        '無框拖曳
        AddHandler MyBase.MouseDown, New MouseEventHandler(AddressOf Me.Form1_MouseDown)
        AddHandler MyBase.Load, New EventHandler(AddressOf Me.Form1_Load)
        AddHandler MyBase.MouseMove, New MouseEventHandler(AddressOf Me.Form1_MouseMove)
        AddHandler MyBase.MouseUp, New MouseEventHandler(AddressOf Me.Form1_MouseUp)
        Me.InitializeComponent()
    End Sub
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None '無框顯示
        Me.Size = New Size(128, 128)
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.TransparencyKey = System.Drawing.SystemColors.Control
        Me.BackgroundImage = My.Resources.love
    End Sub
    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        If (e.Button = MouseButtons.Left) Then
            Me.Check = 1
            Me.temp_x = e.X
            Me.temp_y = e.Y
        End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        If ((Me.Check = 1) And (e.Button = MouseButtons.Left)) Then
            Me.pointTemp.X = (Me.pointTemp.X + (e.X - Me.temp_x))
            Me.pointTemp.Y = (Me.pointTemp.Y + (e.Y - Me.temp_y))
            Me.Location = Me.pointTemp
        End If
    End Sub
    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
        If (e.Button = MouseButtons.Left) Then
            Me.Check = 0
            Me.temp_x = 0
            Me.temp_y = 0
        End If
    End Sub
End Class
範例下載

 


如有錯誤 歡迎指正