问题
I have to track the running time of a program. That program exposes the following window
At the same time I launch my program which in a timer does:
private void TimerCheckGroups_Tick(object sender, EventArgs e)
{
IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Execution");
if (windowPtr != IntPtr.Zero)
Console.Beep();<--------
}
But the beep line never gets hit. Have I misunderstood the meaning of a window caption?
--ADD-- I'll try to make the execution phases clearer.
Startup----> launch my logger.
User-------> launches program A that launches program B (not visible) that launches window C. C has caption Execution.
When I launch the solution proposed by dontbyteme the only the B program appears so only 1 window.
In short
logger: not visible since it's a tray program
program A: visible since it's the main program
program B: not visible since it's set to Notvisible
program C: not visible why?!?!?!?
--SOLUTION THANX TO JARRETT--
logger stays idle with a timer monitoring processes
program A starts but nobody cares about it. Then program A launches program B
when program B is awake i find the window and start logging
回答1:
The following question addresses how to find out when programs launch. Detecting the launch of a application Also, you can enumerate windows on your machine with a dll import and using EnumWindows. Sample pInvokes that will help you are listed.
[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);
回答2:
You can try getting the window by running through each window and compare to the title:
foreach(Window window in Application.Current.Windows)
{
if(window.Title == "Execution")
{
Console.Beep();
// ...
}
}
The Title
property is what you called Caption
.
来源:https://stackoverflow.com/questions/34417158/find-window-by-caption-what-is-the-caption-of-the-window