[分享][VB.Net]可以隨意拖動的窗體類

[分享]可以隨意拖動的窗體類
'此類封裝完整,使用的時候只需inherits shapedForm

Imports System.Drawing.Drawing2D

Public Class ShapedForm
    Inherits System.Windows.Forms.Form
    ' 可以在子類窗體Load事件之前賦值
    Public oFormPath As GraphicsPath
    Private oOriginalRegion As Region = Nothing
    ' 用於窗體移動
    Private bFormDragging As Boolean = False
    Private oPointClicked As Point

#Region " Windows 窗體設計器生成的代碼 "

    Public Sub New()
        MyBase.New()

        '該調用是 Windows 窗體設計器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 調用之後添加任何初始化

    End Sub

    '窗體重寫 dispose 以清理組件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗體設計器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下過程是 Windows 窗體設計器所必需的
    '可以使用 Windows 窗體設計器修改此過程。
    '不要使用代碼編輯器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub ShapedForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' 給子類提供一個設置窗體形狀的機會
        Me.SetInitialFormShape()
        If Not Me.oFormPath Is Nothing Then
            Me.AssignShapePath()
        End If
    End Sub
    Public Sub AssignShapePath()
        If Me.oOriginalRegion Is Nothing Then
            Me.oOriginalRegion = Me.Region
        End If
        Me.Region = New Region(Me.oFormPath)
        Me.Invalidate()
    End Sub
    Public Sub ResetShape()
        Me.Region = Me.oOriginalRegion
        Me.Invalidate()
    End Sub
    Public Overridable Sub SetInitialFormShape()
        ' 這個方法用來讓子類覆蓋
    End Sub
    Private Sub ShapedForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
       Handles MyBase.MouseDown
        Me.bFormDragging = True
        Me.oPointClicked = New Point(e.X, e.Y)
    End Sub
    Private Sub ShapedForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
       Handles MyBase.MouseUp
        Me.bFormDragging = False
    End Sub
    Private Sub ShapedForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
       Handles MyBase.MouseMove
        If Me.bFormDragging Then
            Dim oMoveToPoint As Point
            ' 以當前鼠標位置爲基礎,找出目標位置
            oMoveToPoint = Me.PointToScreen(New Point(e.X, e.Y))
            ' 根據開始位置作出調整
            oMoveToPoint.Offset(Me.oPointClicked.X * -1, (Me.oPointClicked.Y + SystemInformation.CaptionHeight + _
               SystemInformation.BorderSize.Height) * -1)
            ' 移動窗體
            Me.Location = oMoveToPoint
        End If
    End Sub
End Class
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章