我将从纯粹的 C 和 Win32 角度回答这个问题,因为我不了解 Delphi 或其库。可以通过 p/invoke 将其转换为 C#,但某些部分可能/将需要非托管。
首先,没有保证。如果目标应用程序正在执行无 Windows 控件(如果不是HWND
每个屏幕控件下都有一个),那么您就很不走运了。这并不少见,所以是的......
第一步,注册一个窗口挂钩,监听目标进程创建的新窗口*:
//dllHMod is an HMODULE that refers to the DLL containing ShellHookProc
HHOOK hook = SetWindowsHookEx(WH_SHELL, ShellHookProc, dllHMod, 0);
// error handling, stashing hook away for unregistering later, etc...
LRESULT CALLBACK ShellHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0) return CallNextHookEx(NULL, nCode, wParam, lParam);
if(nCode == HSHELL_WINDOWCREATED)
{
WindowCreate((HWND)wParam);
}
return 0;
}
WindowCreated(HWND)
GetWindowThreadProcessId
如果正确的进程(通过 确定)拥有它,则应将 HWND 隐藏起来。此时,您将能够获得目标进程拥有的每个顶级窗口。请注意,注册全局钩子会带来显着的性能损失,并不是说它对你的情况真的很重要,但你应该期待它。
现在是有趣的部分。没有可靠的方法来判断窗口何时完全构建,或何时完成渲染(有一些方法可以判断它何时开始渲染,但这并没有真正帮助)。我的建议,猜。只需在那里随意等待,然后尝试枚举所有子窗口。
To enumerate child windows (if you know enough about the target window, there are better ways to do this; but I'm assuming a search is easiest):
//targetHWND is an HWND of one of the top-level windows you've found
EnumChildWindows(targetHWND, ChildWindowCallback, NULL);
//more code...
BOOL ChildWindowCallback(HWND window, LPARAM ignored)
{
if(IsTargetWindow(window)) { /* Do something */ }
return TRUE;
}
Implementing IsTargetWindow
is another tricky part. Hopefully you'll find some reliable test for doing so (like checking the class name, window name, style, something; look at GetWindowInfo
).
Once you have the window you want to monitor, you can use SetWindowLongPtr
and GWLP_WNDPROC
to watch all messages it receives. This will require code injection (and thus unmanaged code) and is awfully low level. I'd advise against it if you could possibly avoid it, but lacking the source...
I think this answers is a decent starting point, but once again this is going to be incredibly painful if its even possible at all. Good luck.
*Alternatively, if you know that the target app doesn't create windows except at startup (or at detectable/predictable points in time) you can use EnumWindows
.