2013年3月27日 星期三

製作可移動的表單

引用來源
--
 Public Class Form1
    '方法1:類別方式
    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2

    'Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    '    Select Case m.Msg
    '        Case WM_NCHITTEST
    '            MyBase.WndProc(m)
    '            If m.Result = HTCLIENT Then m.Result = HTCAPTION
    '        Case Else
    '            MyBase.WndProc(m)
    '    End Select
    'End Sub
    '----------------
    '方法2:利用 Panel1 來移動
    Private oOriginalRegion As Region = Nothing
    Private bFormDragging As Boolean = False
    Private oPointClicked As Point

    Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        Me.bFormDragging = True
        Me.oPointClicked = New Point(e.X, e.Y)
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.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

    Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
        Me.bFormDragging = False
    End Sub

End Class

沒有留言:

張貼留言