C# 按标题查找窗口窗口的标题是什么?

C# 按标题查找窗口窗口的标题是什么?,c#,wpf,window,caption,C#,Wpf,Window,Caption,我必须跟踪程序的运行时间。 该程序公开以下窗口 同时,我启动我的程序,该程序在计时器中执行以下操作: private void TimerCheckGroups_Tick(object sender, EventArgs e) { IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Execution"); if (windowPtr != IntPtr.Zero) Console.Beep();<-------- }

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

同时,我启动我的程序,该程序在计时器中执行以下操作:

private void TimerCheckGroups_Tick(object sender, EventArgs e)
{
  IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Execution");
  if (windowPtr != IntPtr.Zero)
    Console.Beep();<--------
}
private void TimerCheckGroups\u勾选(对象发送方,事件参数e)
{
IntPtr windowPtr=FindWindowByCaption(IntPtr.Zero,“执行”);
if(windowPtr!=IntPtr.Zero)

Console.Beep();您可以通过运行每个窗口并与标题进行比较来尝试获取窗口:

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

Title
属性就是您所说的
Caption

以下问题涉及如何确定程序何时启动。此外,您还可以使用dll导入和EnumWindows枚举计算机上的窗口。下面列出了将帮助您的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);

你能让你的程序启动执行程序吗?如果你必须手动启动这两个程序,你的时间将不正确。另外,通过让你的跟踪程序启动执行程序,你可以准确地知道它启动的时间。不,我不能。有窗口执行的程序是由用户启动的。而我的程序是一个记录器,所以它是启动的d在窗口启动时。根据您对编组的熟悉程度,您是否查看过“EnumWindows我使用它查找打开的窗口。它位于user32.dll中。这将为您提供打开的窗口,但无法解决新程序启动时的问题。此外,这篇文章可能对您有用。@Jarretrobertson thanx是我的这是个问题,请看我的编辑,因为我已经按照你的提示去解决了。我希望你发布一个解决方案,说明这个问题同时涉及到启动程序,我认为你应得的。