9

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
  • 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.
    – vcsjones
    Aug 29, 2014 at 15:20
  • See this stackoverflow.com/questions/7268302/….
    – aybe
    Aug 29, 2014 at 15:44
  • @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.
    – bob
    Aug 29, 2014 at 16:01
  • Use it in conjunction with @vcsjones hint.
    – aybe
    Aug 29, 2014 at 16:57
  • Do you know the title of the window you want to find? Aug 29, 2014 at 20:52

1 Answer 1

5

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.