How do I see if my form is currently on top of the other ones?
Asked Answered
P

4

5

Basically, how do I tell if my program is layered above all the other ones?

Placeeda answered 21/8, 2012 at 4:38 Comment(0)
N
13

A fairly simple way is to P/Invoke GetForegroundWindow() and compare the HWND returned to the application's form.Handle property.

using System;
using System.Runtime.InteropServices;

namespace MyNamespace
{
    class GFW
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        public bool IsActive(IntPtr handle)
        {
            IntPtr activeHandle = GetForegroundWindow();
            return (activeHandle == handle);
        }
    }
}

Then, from your form:

if (MyNamespace.GFW.IsActive(this.Handle))
{
  // Do whatever.
}
Nekton answered 21/8, 2012 at 4:42 Comment(3)
So this will be run instantly when it becomes the topmost form?Placeeda
No, the code sample above just checks to see whether you have the foreground window. To execute code when you become the foreground window you'll have to use SetWinEventHook.Nekton
Thanks so much, I've taken an hours for search this solution on google but now you're my heroDogeatdog
F
1

You can use:

if (GetForegroundWindow() == Process.GetCurrentProcess().MainWindowHandle)
{
     //do stuff
}

WINAPI imports (at class level):

[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool GetForegroundWindow();

Assign a property to hold the value, and add the check to the form's GotFocus event through IDE, or after InitializeComponent();

e.g.:

//.....
InitalizeComponent();
this.GotFocus += (myFocusCheck);
//...

private bool onTop = false;

private void myFocusCheck(object s, EventArgs e)
{
    if(GetFore......){ onTop = true; }
}
Frannie answered 21/8, 2012 at 5:0 Comment(1)
Two problems - I need it to tell when it becomes the topmost window, and I don't know where to put the imports.Placeeda
H
0

If your window inherits form, you can check Form.Topmost property

Hieroglyphic answered 23/7, 2014 at 7:46 Comment(0)
A
0

A good solution is given by this answer to an identical question: https://mcmap.net/q/242159/-determine-if-current-application-is-activated-has-focus

/// <summary>Returns true if the current application has focus, false otherwise</summary>
public static bool ApplicationIsActivated()
{
    var activatedHandle = GetForegroundWindow();
    if (activatedHandle == IntPtr.Zero) {
        return false;       // No window is currently activated
    }

    var procId = Process.GetCurrentProcess().Id;
    int activeProcId;
    GetWindowThreadProcessId(activatedHandle, out activeProcId);

    return activeProcId == procId;
}


[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

The currently accepted solution by Euric doesn't work if your program shows a dialog box or has detachable windows (like if you use a windows docking framework).

Affinity answered 15/12, 2015 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.