VB中實現窗體自動隱藏 (轉)

gugu99發表於2008-04-27
VB中實現窗體自動隱藏 (轉)[@more@]

OICQ時的窗體自動隱藏功能,用或CBC都可以很簡單地實現,前幾天看到有一則用CBC實現的例子,便想用VB實現一下,可惜當窗體上放滿時,FORM的MOUSEMOVE事件不能很好地觸發,所以只好用以下的笨辦法,現把程式碼貼上,窗體上需放一TIMER控制元件,Interval屬性為200。大夥如有好的意見,不妨貼出來,讓我學習學習。

Option Explicit

Private Declare Function GetCursorPLib "user32" (lpPoint As POINT) 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 RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type
Private Type POINTAPI
  X As Long
  Y As Long
End Type

Private Const HWND_TOPMOST = -1
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const HWND_TOP = 0
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40


Private Sub Form_Load()
'窗體放在最前面
  SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

Private Sub Timer1_Timer()
  Dim p As POINTAPI
  Dim f As RECT
  GetCursorPos p  '得到MOUSE位置
  GetWindowRect Me.hwnd, f  '得到窗體的位置
  If Me.tate <> 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 Me.Top < 0 Then
  Me.Top = -10
  Me.Show
  ElseIf Me.Left < 0 Then
  Me.Left = -10
  Me.Show
  ElseIf Me.Left + Me.Width >= Screen.Width Then
  Me.Left = Screen.Width - Me.Width + 10
  Me.Show
  End If
 
  Else
  If f.Top <= 4 Then
  Me.Top = 40 - Me.Height
  ElseIf f.Left <= 4 Then
  Me.Left = 40 - Me.Width
  ElseIf Me.Left + Me.Width >= Screen.Width - 4 Then
  Me.Left = Screen.Width - 40
  End If
  End If
  End If

End Sub


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1003055/,如需轉載,請註明出處,否則將追究法律責任。

相關文章