Using GetForegroundWindow causes the if statement to check the current user window

I need to check which window the user has selected now and do something if they have a specific program.

I have not used the GetForegroundWindow function before and cannot find any information on how to use it this way.

I just need to map the current window to see if its specific program is there. However, the GetForegroundWindow function does not return a string or int. So basically I don't know how to find out the window value of the program I want to compare with.

I currently have some 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 ideally:

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

      

I hope that the GetForegroundWindow value / object is unique for each program and does not work in some way, that each specific program / window has a different value every time.

I also do this as part of the window form, although I doubt it matters.

- Thanks for the help

Edit: This way works and uses a snippet of the current window, making it perfect for validating the correctness of the window:

        [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 
        }

      

+3


source to share


1 answer


It has 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 the 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;
        }
     }

      

Walter

+3


source







All Articles