Use GetForegroundWindow result in an if statement to check user's current window
Asked Answered
I

1

11

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 
        }
Interpellant answered 29/8, 2014 at 15:9 Comment(6)
You could then platform invoke GetWindowThreadProcessId to get the process ID from the IntPtr of GetForegroundWindow. Once you have a process ID, you can examine it with the Process class.Shive
See this #7268802.Oakley
@Aybe That just gets a list of all windows doesn't it, I need to get only the currently selected window. Unless I'm missing something, because simply doing "if(process.MainWindowTitle=="Program X")" would be great.Interpellant
Use it in conjunction with @Shive hint.Oakley
Do you know the title of the window you want to find?Dada
@valter Yup, the window will always have the same.Interpellant
S
7

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

Shoifet answered 29/8, 2014 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.