Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim MousePos As POINTAPI
Const FLAT_TOP = 1
Const FLAT_BOTTOM = 2
Const FLAT_LEFT = 4
Const FLAT_RIGHT = 8
Dim FlatFlag As Integer
Public Function PtInRect(r As RECT, pt As POINTAPI) As Boolean
If r.Left <= pt.X And pt.X <= r.Right And r.Top <= pt.Y And pt.Y < r.Bottom Then PtInRect = True Else PtInRect = False
End Function
Public Sub CursorPosTimer()
Dim r As RECT
Dim lRight As Long
Dim ir As Boolean
GetWindowRect Me.hwnd, r
ir = PtInRect(r, MousePos)
If (FlatFlag And FLAT_LEFT) = FLAT_LEFT Then
If MousePos.X <= 2 And ir Then
Me.Left = 0
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
If Me.Left = 0 And Not ir Then
Me.Left = -Me.Width + 2 * Screen.TwipsPerPixelX
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
End If
End If
lRight = Screen.Width / Screen.TwipsPerPixelX - Me.Width / Screen.TwipsPerPixelX
If (FlatFlag And FLAT_RIGHT) = FLAT_RIGHT Then
If MousePos.X >= lRight - 2 And ir Then
Me.Left = lRight * Screen.TwipsPerPixelX
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
If Me.Left = lRight * Screen.TwipsPerPixelX And Not ir Then
Me.Left = Screen.Width - 2 * Screen.TwipsPerPixelX
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
End If
End If
If (FlatFlag And FLAT_TOP) = FLAT_TOP Then
If MousePos.Y <= 2 And ir Then
Me.Top = 0
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
If Me.Top = 0 And Not ir Then
Me.Top = -Me.Height + 2 * Screen.TwipsPerPixelY
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
End If
End If
End Sub
Private Sub tmrMousePos_Timer()
GetCursorPos MousePos
CursorPosTimer
End Sub
QQ窗体的收缩隐藏:
timer控件捕捉 MouseExit 事件
'声明
Public Declare Function GetCursorPos Lib "user32" (lpPoint As _pointapi) As Long
Type pointapi
x As Long
y As Long
End Type
'过程
Private Sub Timer1_Timer()
Dim a As Long
a = GetCursorPos(mouse)
If (mouse.x > frmMain.ScaleWidth) Or (mouse.y > _frmMain.ScaleHeight ) Then
frmMain.Top = (0 - frmMain.Height) + 100
End If
If mouse.y < 30 Then
frmMain.Top = 10 '理论上为0