通过标题查找 Window window 的标题是什么?

Find Window By Caption what is the caption of the window?

我必须跟踪程序的 运行 时间。 该程序公开了以下 window

同时我启动了我的程序,它在计时器中执行:

private void TimerCheckGroups_Tick(object sender, EventArgs e)
{
  IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Execution");
  if (windowPtr != IntPtr.Zero)
    Console.Beep();<--------
}

但蜂鸣线永远不会被击中。 我是否误解了 window 标题的含义?

--添加-- 我会尽量让执行阶段更清晰。

启动----> 启动我的记录器。

用户--------> 启动程序 A,程序 A 启动程序 B(不可见),程序 B(不可见)启动 window C。C 有标题执行。

当我启动dontbyteme提出的解决方案时,只出现了B程序,所以只有1个window。

简而言之


--感谢 JARRETT 的解决方案--

您可以尝试通过每个 window 通过 运行 获取 window 并与标题进行比较:

    foreach(Window window in Application.Current.Windows)
    {
        if(window.Title == "Execution")
        {
            Console.Beep();
            // ...
        }
    }

Title属性就是你所说的Caption

以下问题解决了如何查明程序启动的时间。 Detecting the launch of a application 此外,您可以通过导入 dll 并使用 EnumWindows 在您的计算机上枚举 windows。列出了对您有帮助的示例 pInvoke。

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int GetWindowTextLength(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern bool IsWindowVisible(IntPtr hWnd);