VB代码问题,类似于QQ的窗体有吸附功能,把窗体移动到屏幕边缘能悬挂起来的…
展开全部
用timer判断form的位置,如果top=0就把top-form1.height'留一点点边缘出来也可以。。。
然后再用getcursorpos获取鼠标位置,如果在窗体下边缘就控制top=0
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Timer1_Timer()
Dim p As POINTAPI, r As RECT
GetCursorPos p
GetWindowRect Me.hwnd, r
If r.Top <= 0 And (Not (p.y < r.Bottom And p.x > r.Left And p.x < r.Right)) Then
Form1.Top = -Form1.Height + 100
End If
If p.x > r.Left And p.x < r.Right And p.y < r.Bottom Then
If r.Top <= 0 Then
SetWindowPos Me.hwnd, -2, r.Left, 0, 0, 0, 1
Else
SetWindowPos Me.hwnd, -2, r.Left, r.Top, 0, 0, 1
End If
End If
End Sub
只做了顶端吸附,侧边和低端也是差不多的
然后再用getcursorpos获取鼠标位置,如果在窗体下边缘就控制top=0
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Timer1_Timer()
Dim p As POINTAPI, r As RECT
GetCursorPos p
GetWindowRect Me.hwnd, r
If r.Top <= 0 And (Not (p.y < r.Bottom And p.x > r.Left And p.x < r.Right)) Then
Form1.Top = -Form1.Height + 100
End If
If p.x > r.Left And p.x < r.Right And p.y < r.Bottom Then
If r.Top <= 0 Then
SetWindowPos Me.hwnd, -2, r.Left, 0, 0, 0, 1
Else
SetWindowPos Me.hwnd, -2, r.Left, r.Top, 0, 0, 1
End If
End If
End Sub
只做了顶端吸附,侧边和低端也是差不多的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询