当用户将其拖动到边缘的5个像素内时,我试图让我的表单捕捉到屏幕的边缘。我的应用程序有多种形式,可用于多个屏幕。下面的代码正确地捕捉到顶部边缘,但是当它捕捉到屏幕的左侧和右侧时,屏幕边缘和窗体之间存在间隙。似乎我的屏幕的左边缘是X = -4而不是X = 0.我如何确定实际边缘或者它总是X = -4。
我已尝试使用不同屏幕的多台PC,但当它卡入屏幕的左侧或右侧边缘时,我一直有间隙。我的表单上的填充设置为0,0,0,0。我也试过使用screen.bounds而不是screen.workingArea但我得到了相同的结果。我正在测试Windows 10上的应用程序
Private Sub Form_Move(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Move
FormSnaptoEdge(Me)
End Sub
Public Sub FormSnaptoEdge(ByVal FormtoSnap As Form)
Dim snaptol As Integer = 5
For Each screen As Screen In screen.AllScreens
Dim screenrect As Rectangle = screen.WorkingArea
If FormtoSnap.Left <= screenrect.Left + snaptol AndAlso FormtoSnap.Left > screenrect.Left - snaptol Then
FormtoSnap.Left = screenrect.Left
ElseIf FormtoSnap.Right >= screenrect.Right - snaptol AndAlso FormtoSnap.Right < screenrect.Right + snaptol Then
FormtoSnap.Left = screenrect.Right - FormtoSnap.Width
End If
If FormtoSnap.Top <= screenrect.Top + snaptol AndAlso FormtoSnap.Top > screenrect.Top - snaptol Then
FormtoSnap.Top = screenrect.Top
ElseIf FormtoSnap.Bottom >= screenrect.Bottom - snaptol AndAlso FormtoSnap.Bottom < screenrect.Bottom + snaptol Then
FormtoSnap.Top = screenrect.Bottom - FormtoSnap.Height
End If
Next
End Sub
先谢谢