0

我编写了以下过程来移动和停靠无边框窗口:

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

当我添加用于停靠的代码并尝试移动表单时,它会移动并停靠,但是当按住鼠标并移动时它会疯狂地闪烁。我不明白为什么会发生这种情况,这是我第一次涉足这样的事情,所以我不确定我哪里出错了。

4

3 回答 3

0

从我所见,您应该首先检查表单是否停靠,然后为其设置正确的位置。就像这样,您首先设置位置,然后设置顶部,因此表单移动了两次并闪烁......

于 2013-11-08T18:40:38.547 回答
0

在带有停靠检查的代码块中,第一个“If”块根据鼠标位置设置为表单的位置,然后在第二个和第三个“If”块中,根据停靠设置位置。这导致表单每次鼠标移动两次左右移动。您需要某种标志来指示表单处于停靠状态,然后在设置此标志时根本不移动表单。

于 2013-11-08T18:42:29.627 回答
0

我遇到了与此类似的问题,结果证明是由表单的TransparencyKey.

尝试删除设置的颜色。

于 2015-05-20T11:46:11.500 回答