Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
If GetActiveWindow() <> hWnd Then End
End Sub
设置timer1时间间隔为几百毫秒,呵呵~~~,给分吧!
呵呵,用TIMER当然可以了,我设的interval为10,但这样会增大系统开销。
给出另一种方法:
Module:
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_ACTIVATE = &H6
Public preWinProc As Long
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_ACTIVATE Then
If wParam = 0 Then Unload Form1
End If
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
Form:
Private Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf wndproc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
ret = SetWindowLong(Form1.hwnd, GWL_WNDPROC, preWinProc)
End Sub