Results 1 to 9 of 9

Thread: Detecting when program/form loses focus

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Detecting when program/form loses focus

    How do one detect that a form lost focus when program loses focus i.e. user go to another program for instance? Program is open and form is open but I go to another program.

    LostFocus event of open form doesn't fire when I click out of my program to another program.

    There is functionality in my program that I need to turn off when the user go to another program for instance and turn it back on when the user comes back to the program. But it's on a form level that I need to turn the functionality on/off.

    Everything work with Activate/Deactivate events when jumping between forms in the program but I can't get it to work when leaving a form open and jumping in/out of the program.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Detecting when program/form loses focus

    WM_ACTIVATEAPP message

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Detecting when program/form loses focus

    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Re: Detecting when program/form loses focus

    Here's what I use and no subclassing required.

    Code:
    Public Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hWnd As Long) As Long

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Detecting when program/form loses focus

    Quote Originally Posted by vb6forever View Post
    Here's what I use and no subclassing required.

    Code:
    Public Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hWnd As Long) As Long
    vb6forever, SetFocus doesn't detect anything though. It was my understanding he wanted to "detect" when the form got/lost focus (across applications), and not actually set it. Maybe I don't understand though.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    Fanatic Member
    Join Date
    Feb 2017
    Posts
    858

    Re: Detecting when program/form loses focus

    Elroy:
    I agree with your reading of OP's question.
    Since I still use VB5 and there is no Validate event, that API allows one to Set the focus back to the last control they just left, and hence some workaround code, since part of his complaint was that LostFocus was Not firing.
    I threw that API out there as "A POSSIBLE", instead of Subclassing, since no code was posted.
    Regards.

  7. #7
    Addicted Member Mikle's Avatar
    Join Date
    Oct 2009
    Location
    Tuapse, Russia
    Posts
    138

    Re: Detecting when program/form loses focus

    Here is a simple example.
    Attached Files Attached Files

  8. #8
    Addicted Member ISAWHIM's Avatar
    Join Date
    Jan 2023
    Posts
    181

    Re: Detecting when program/form loses focus

    Chances are, the reason the FORM didn't throw a "lost-focus" event, was because it didn't have it. (But, I understand windows, and know that once a program actually looses focus, it can ALSO loose the ability to react too. Assuming it DID have focus, and the event just wasn't getting handled by VB6 in time.)

    The focus was probably, I assume, on a control within the form. It (the control that had focus) would be the thing which looses it, not the form. The form already "lost focus", once some other object "got focus".

    If you wanted to see when the whole program/window "has focus", you would have to actively monitor it with this...
    "GetForegroundWindow" function

    Reason being... "Windows" don't have a focus, really. The "focus", is "active or inactive". (Active being "focused as the input program")

    Or, you would have to monitor each item that can "get focus", in your program, and add code to the "lost focus" and "got focus" functions. Some code that set a value for when it has focus and looses focus. When all values are 0, then nothing "has focus" in your program, so the "window" has lost focus. If the value is > 1, than SOMETHING in the form/window has focus.
    Last edited by ISAWHIM; Mar 27th, 2023 at 01:17 PM.

  9. #9
    Addicted Member Mikle's Avatar
    Join Date
    Oct 2009
    Location
    Tuapse, Russia
    Posts
    138

    Re: Detecting when program/form loses focus

    Quote Originally Posted by ISAWHIM View Post
    If you wanted to see when the whole program/window "has focus", you would have to actively monitor it with this...
    "GetForegroundWindow" function
    It is for this case that I gave an example without a waiting cycle with the GetForegroundWindow() function.

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