Use Getforegroundwindow Result in an If Statement to Check User's Current Window

I need to check what window the user currently has selected, and do stuff if they have a specific program selected.

I haven't used the GetForegroundWindow function before, and can't find any information on how to use it in this manner.

I simply need an if comparing the current window to see if its a specific program. However the GetForegroundWindow function doesn't give back a string or int it seems. So mainly I don't know how to find out the value of the program window I want to compare it to.

I currently have the code to get the current window:

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

    IntPtr selectedWindow = GetForegroundWindow();

I need to be able to apply it as follows ideally:

    If (selectedWindow!="SpecificProgram")
    {
        <Do this stuff>
    } 

I'm hoping the GetForegroundWindow value/object is unique to each program and doesn't function in some way that each specific program/window has different values each-time.

I'm also doing this as part of a windows form though I doubt it matters.

-Thanks for any help

Edit: This way works, and uses the tile of the current window, which makes it perfect for checking if the window is right easily:

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        private string GetActiveWindowTitle()
        {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);
            IntPtr handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }
            return null;
        }

and then I can just do:

        if (GetActiveWindowTitle()=="Name of Window")
        {
            DoStuff.jpg 
        }
6

1 Answer

It has some code but it works:

    #region Retrieve list of windows

    [DllImport("user32")]
    private static extern int GetWindowLongA(IntPtr hWnd, int index);

    [DllImport("USER32.DLL")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("USER32.DLL")]
    private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

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

    private const int GWL_STYLE = -16;

    private const ulong WS_VISIBLE = 0x10000000L;
    private const ulong WS_BORDER = 0x00800000L;
    private const ulong TARGETWINDOW = WS_BORDER | WS_VISIBLE;

    internal class Window
    {
        public string Title;
        public IntPtr Handle;

        public override string ToString()
        {
            return Title;
        }
    }

    private List<Window> windows;

    private void GetWindows()
    {
        windows = new List<Window>();
        EnumWindows(Callback, 0);
    }

    private bool Callback(IntPtr hwnd, int lParam)
    {
        if (this.Handle != hwnd && (GetWindowLongA(hwnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW)
        {
            StringBuilder sb = new StringBuilder(100);
            GetWindowText(hwnd, sb, sb.Capacity);

            Window t = new Window();
            t.Handle = hwnd;
            t.Title = sb.ToString();
            windows.Add(t);
        }

        return true; //continue enumeration
    }

    #endregion

And to check user window:

    IntPtr selectedWindow = GetForegroundWindow();
    GetWindows();

    for (i = 0; i < windows.Count; i++)
    {
        if(selectedWindow == windows[i].Handle && windows[i].Title == "Program Title X")
        {
             //Do stuff

             break;
        }
     }

Valter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest