【问题标题】:Use GetForegroundWindow result in an if statement to check user's current window在 if 语句中使用 GetForegroundWindow 结果来检查用户的当前窗口
【发布时间】:2014-08-29 15:09:40
【问题描述】:

我需要检查用户当前选择了哪个窗口,如果他们选择了特定的程序,我需要做一些事情。

我之前没有使用过GetForegroundWindow函数,也找不到任何关于如何以这种方式使用它的信息。

我只需要一个 if 来比较当前窗口,看看它是否是一个特定的程序。但是,GetForegroundWindow 函数似乎并没有返回字符串或 int。所以主要是我不知道如何找出我想与之比较的程序窗口的值。

我目前有获取当前窗口的代码:

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

    IntPtr selectedWindow = GetForegroundWindow();

我需要能够按如下理想方式应用它:

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

我希望 GetForegroundWindow 值/对象对于每个程序都是唯一的,并且不会以某种方式起作用,因为每个特定程序/窗口每次都有不同的值。

虽然我怀疑它是否重要,但我也将其作为 Windows 窗体的一部分来执行。

-感谢您的帮助

编辑:这种方式有效,并且使用当前窗口的磁贴,这使得它非常适合轻松检查窗口是否正确:

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

然后我就可以这样做了:

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

【问题讨论】:

  • 然后您可以平台调用GetWindowThreadProcessId 从 GetForegroundWindow 的 IntPtr 获取进程 ID。获得进程 ID 后,您可以使用 Process 类对其进行检查。
  • @Aybe 这只是获取所有窗口的列表,不是吗,我只需要获取当前选定的窗口。除非我遗漏了什么,因为简单地执行“if(process.MainWindowTitle=="Program X")”会很棒。
  • 与@vcsjones 提示一起使用。
  • 您知道要查找的窗口的标题吗?

标签: c# winforms if-statement dllimport


【解决方案1】:

它有一些代码,但它可以工作:

    #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

并检查用户窗口:

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

瓦尔特

【讨论】:

    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多