Surrendermonkey
February 18th, 2000, 05:03 AM
I am writing a fairly database unobtrusive app in VB 6, but need to inform my app when it "loses the focus" - (I realise this is the wrong phrase) -, either by the user clicking another application window, taskbar icon or whatever. The Form.DeActivate events work perfectly when the activated window is from the same app, but not otherwise. How is this to be done?
Thanks for reading,
surrendermonkey
Crazy D
February 18th, 2000, 05:12 AM
Subclass the form and catch the WM_ACTIVATE event.
Crazy D :-)
"One ring rules them all"
Surrendermonkey
February 18th, 2000, 07:12 AM
"Sub-class the form" Fair enough. What's the first Step? Are we walking CFormEvents and related business? If so, could you drop me a URL so I can download the class, 'cause I've been looking into it and indeed for it for a while? Or just mail it me?
Thanks,
surrendermonkey
Chris Eastwood
February 18th, 2000, 07:42 AM
Here's some code I just knocked up - it's based on Aaron Youngs subclassing example posted earlier today:
1. Place the following code in a Module :
option Explicit
'
private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, _
byval dwNewLong 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
'
private Const GWL_WNDPROC = (-4)
private Const WM_ACTIVATE = &H6
private lPrevWndFunc as Long
'
private Function WindowProc(byval hwnd as Long, byval Msg as Long, _
byval wParam as Long, byval lParam as Long) as Long
'
Dim iIndex as Long
'
' Here's where you find out if it's activated / deactivated
'
If Msg = WM_ACTIVATE then
If wParam = 0 then
Debug.print "App. Lost Focus / Deactivated"
else
Debug.print "App. Activated"
End If
End If
'
WindowProc = CallWindowProc(lPrevWndFunc, hwnd, Msg, wParam, lParam)
End Function
'
'
public Sub SubClassIt(byval hwnd as Long, byval Add as Boolean)
If Add then
lPrevWndFunc = SetWindowLong(hwnd, GWL_WNDPROC, _
AddressOf WindowProc)
else
Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndFunc)
End If
End Sub
2. In Form1, paste the following code :
option Explicit
'
private Sub Form_Load()
SubClassIt me.hwnd, true
End Sub
'
private Sub Form_Unload(Cancel as Integer)
SubClassIt me.hwnd, false
End Sub
As with all programs that use subclassing - take great care that you don't hit the 'end/stop' button in VB - make sure you exit the program cleanly (and that the routine in Form_Unload finishes) - otherwise you'll end up with a heap of GPF's and possibly a reboot if you using Win95/98.
You could possibly have problems debugging also - remember to save your code regularly if you use subclassing.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb