Is there any way of making the Form be hiden or end as soon as the user clicks on another window or the desktop.
This would act very like the volumn control on the smallicons part of the task bar.
Thanks
Rob
Printable View
Is there any way of making the Form be hiden or end as soon as the user clicks on another window or the desktop.
This would act very like the volumn control on the smallicons part of the task bar.
Thanks
Rob
I am a newbie - but does "Unload ME" do the trick??
Hope this helps
Have you tried using the Lost Focus procedure of the form and place something like
Sub Form_LostFocus()
Me.Unload
End Sub
Sub Form_Unload ()
Set Formname=nothing
End Sub
parkes, there is no such thing as Me.Unload. You have to use the Unload statement. Unload Me.
THEROB, there are a few ways of doing this, you can do it the easy way, with a timer and API (GetForegroundWindow):
Or you can Subclass which will detect whether the form has lost focus. This is the best way to go rather than the first example.Code:Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Sub Timer1_Click()
Dim hforewnd As Long
Dim handl As Long
handl = Me.hWnd
hforewnd = GetForegroundWindow()
If handl <> hforewnd Then
Unload Me
'Me.WindowState = 1
'Or
'Me.Hide
Else
End If
End Sub
Careful when Subclassing, it crashes VB if used wrong.Code:'Module code:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
Private 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
' This is used with the SetWindowLong API function.
Const GWL_WNDPROC = -4
Public Const WM_KILLFOCUS = &H8
Dim saveHWnd As Long ' The handle of the subclassed window.
Dim oldProcAddr As Long ' The address of the original window procedure
Sub StartSubclassing(ByVal hWnd As Long)
saveHWnd = hWnd
oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Sub StopSubclassing()
SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
End Sub
Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' Send the message to the original window procedure, and then
' return Windows the return value from the original procedure.
WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
Select Case uMsg
Case WM_KILLFOCUS
Unload Form1
Set Form1 = Nothing
'^You can change this
'Minimized:
'Form1.WindowState = 1
'Hidden:
'Form1.Hide
End Select
End Function
'Form code:
Private Sub Form_Load()
StartSubclassing Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
StopSubclassing
End Sub
NEVER USE THE END STATEMENT WHILE SUBCLASSING.