CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: App Deactivated

  1. #1
    Join Date
    Sep 1999
    Location
    Leeds U.K. (Proud to be Sheffield Born)
    Posts
    202

    App Deactivated

    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


  2. #2
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: App Deactivated

    Subclass the form and catch the WM_ACTIVATE event.

    Crazy D :-)
    "One ring rules them all"

  3. #3
    Join Date
    Sep 1999
    Location
    Leeds U.K. (Proud to be Sheffield Born)
    Posts
    202

    Re: App Deactivated

    "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


  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: App Deactivated

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured