'*************************************************************
'说明: 在VB6.0中让窗体像QQ一样自动隐藏
'备注:测试代码的工程名为:HideFrmLikeQQ
'原理:捕获窗体的鼠标按下事件并将HTCAPTION消息发送给窗口
'作者:袁培荣 yuanpeirong@vip.qq.com
'修改时间:2011年09月26日
'*************************************************************
'使用方法:在Timer控件的事件里调用:Call YPRSubFormHide(FormA)
'FormA为VB窗体名称,注意一定要设置Timer控件开启,响应间隔推荐0.1秒
'声明模块名称如下:
Attribute VB_Name = "YuanPeirongSoftPublicSubFormHide"
'第一步:声明Windows API函数并定义相应结构体,代码如下:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
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 POINTAPI '用于API函数GetCursorPos
x As Long
y As Long
End Type
Private Type RECT '用于API函数GetWindowRect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Sub YPRSubFormHide(formA As Form) '让VB窗口自动靠边隐藏
Dim p As POINTAPI
Dim f As RECT
GetCursorPos p '得到MOUSE位置
GetWindowRect formA.hwnd, f '得到窗体的位置
If formA.WindowState <> 1 Then
If p.x > f.Left And p.x < f.Right And p.y > f.Top And p.y < f.Bottom Then
'MOUSE 在窗体上
If formA.Top < 0 Then
formA.Top = -10
formA.Show
ElseIf formA.Left < 0 Then
formA.Left = -10
formA.Show
ElseIf formA.Left + formA.Width >= Screen.Width Then
formA.Left = Screen.Width - formA.Width + 10
formA.Show
End If
Else
If f.Top <= 4 Then
formA.Top = 40 - formA.Height
ElseIf f.Left <= 4 Then
formA.Left = 40 - formA.Width
ElseIf formA.Left + formA.Width >= Screen.Width - 4 Then
formA.Left = Screen.Width - 40
End If
End If
End If
End Sub