我编写了以下过程来移动和停靠无边框窗口:
Public Class frmNavigation
    Inherits Form
    'Declarations to allow form movement on mouse down
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Dim Xs As Integer
    Dim Ys As Integer
    Dim DockScale As Integer
Private Sub frmNavigation_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
        'This procedure allows the user to move the form when the 
        'mouse button is down. The form does not have borders, so it
        'needs to be coded to move.
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub
    Private Sub frmNavigation_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
        'This procedure allows the user to move the form when the 
        'mouse button is up. The form does not have borders, so it
        'needs to be coded to move.
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub
    Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
        'This procedure allows the user to move the form when the 
        'mouse button is dragging the form. The form does not have borders, so it
        'needs to be coded to move.
        Dim curScreen As Screen
        curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
        Dim height As Integer = curScreen.Bounds.Height
        Dim width As Integer = curScreen.Bounds.Width
        width = curScreen.WorkingArea.Width
        height = curScreen.WorkingArea.Height
        If IsFormBeingDragged Then
            Dim temp As System.Drawing.Point = New System.Drawing.Point()
            Xs = MouseDownX
            Ys = MouseDownY
            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
End Sub
End Class
到目前为止,这按设计工作,它移动表单没有任何问题。当我添加代码以将表单停靠在鼠标移动事件下时,问题就开始了:
Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
    'This procedure allows the user to move the form when the 
    'mouse button is dragging the form. The form does not have borders, so it
    'needs to be coded to move.
    Dim curScreen As Screen
    curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
    Dim height As Integer = curScreen.Bounds.Height
    Dim width As Integer = curScreen.Bounds.Width
    width = curScreen.WorkingArea.Width
    height = curScreen.WorkingArea.Height
    If IsFormBeingDragged Then
        Dim temp As System.Drawing.Point = New System.Drawing.Point()
        Xs = MouseDownX
        Ys = MouseDownY
        temp.X = Me.Location.X + (e.X - MouseDownX)
        temp.Y = Me.Location.Y + (e.Y - MouseDownY)
        Me.Location = temp
        temp = Nothing
    End If
    If IsFormBeingDragged = True And e.Button = MouseButtons.Left Then
        'if the drag flag is true and left mouse button is pressed...
        'set Top side docking
        If Me.Top + (MouseDownY - Ys) < DockScale Then
            Me.Top = 0
            Exit Sub
        End If
        'set bottom side docking
        If Me.Top + (MouseDownY - Ys) + Me.Height > (height - DockScale) Then
            Me.Top = height - Me.Height
            Exit Sub
        End If
        'move the form finally
        Me.Left = Me.Left + (MouseDownX - Xs)
        Me.Top = Me.Top + (e.Y - Ys)
    End If
End Sub
当我添加用于停靠的代码并尝试移动表单时,它会移动并停靠,但是当按住鼠标并移动时它会疯狂地闪烁。我不明白为什么会发生这种情况,这是我第一次涉足这样的事情,所以我不确定我哪里出错了。