0

I have a lots of problem to distinguish such a simple thing.

I need to know if a form is currently in front of everything, the one which receives key entries.

I have no way to know if it is.

I can check if not minimized. But then it may just be behind other windows, or just not being selected (for example it is openend, desktop is behind, you click on desktop, then you still see the application, but it doesn't receive key inputs).

The property focus is irrevelant for this.

Here is the code

    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);

        if (this.Focused)
        {
            gotFocus = true;
            // never reaches tis
        }
Cher
  • 2,789
  • 10
  • 37
  • 64
  • That's exactly what Focus means. – SLaks Jul 27 '16 at 16:14
  • I do this: if (this.Focused) and condition inside isn't done – Cher Jul 27 '16 at 16:16
  • What are you talking about? What are you checking? That should not happen. – SLaks Jul 27 '16 at 16:17
  • 1
    `if (ActiveForm == this) {...}` – LarsTech Jul 27 '16 at 16:18
  • I added my code to be more precise – Cher Jul 27 '16 at 16:19
  • @LarsTech this is true even when form is sometime minimized – Cher Jul 27 '16 at 16:22
  • @Cher It depends on what is your definition of being active. Add some other criteria to the code that `LarsTech` offered if you need. – Reza Aghaei Jul 27 '16 at 16:23
  • Create a flag, and use Enter and Leave events to set that flag. – Pau C Jul 27 '16 at 16:24
  • well a form may be active but behing another form. The event OnActivated is sometime triggered even if form is not in front – Cher Jul 27 '16 at 16:25
  • 1
    The Deactivate event ought to be more interesting to you, albeit that it does not distinguish between another form in your app getting activated or another window that belongs to another process. There is no event for the WM_ACTIVATEAPP message, it is a rather dangerous one. You can pinvoke [GetForegroundWindow](http://pinvoke.net/default.aspx/user32/GetForegroundWindow.html) and compare it to your form's Handle property. – Hans Passant Jul 27 '16 at 16:27
  • @HansPassant trying it right now, feel like it could work – Cher Jul 27 '16 at 16:36
  • @HansPassant thank you!! it worked after so much hours!! If you just post it as answers I'll accept it. Else I want give the code here. Thanks a million!! – Cher Jul 27 '16 at 16:47
  • Good, you know how to do it, you can now also complete your Q+A and post the answer. – Hans Passant Jul 27 '16 at 16:48
  • @HansPassant wrote answer. Let me know if something could be improved – Cher Jul 27 '16 at 17:27

1 Answers1

1

Check if window is the current active window.

Code:

using System.Runtime.InteropServices; // To use DllImport

...

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

        if ((IntPtr)GetForegroundWindow() == this.Handle)
        {
            // Do stuff
        }

See: Use GetForegroundWindow result in an if statement to check user's current window

Community
  • 1
  • 1
Cher
  • 2,789
  • 10
  • 37
  • 64