我编写了以下程序来移动和停靠无边框窗口:
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
当我添加对接代码时,我尝试移动表单,它会移动并停靠,但是当按住鼠标并移动时,它会像疯了一样闪烁。我不明白为什么会这样,这是我第一次涉足这样的事情,所以我不确定我哪里出错了。
答案 0 :(得分:0)
从我看到你应首先检查表格是否已停靠,然后为其设置正确的位置。 就像这样你首先设置位置,然后你设置顶部,所以表格被移动两次,它闪烁......
答案 1 :(得分:0)
在使用停靠点检查的代码块中,第一个“If”块根据鼠标位置设置为窗体的位置,然后在第2个和第3个“If”块中根据对接设置位置。这导致每个鼠标移动形成两次移动。您需要某种标志来指示表单处于停靠状态,然后在设置此标志时根本不移动表单。
答案 2 :(得分:0)
我遇到了与此类似的问题,结果是由TransparencyKey
形式引起的。
尝试删除设定的颜色。