在VB6.0中讓窗體像QQ同樣自動隱藏

'*************************************************************
'說明: 在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
相關文章
相關標籤/搜索