简体   繁体   English

C ++ FindWindow查找当前窗口

[英]C++ FindWindow to find current window

I have a program that opens a window (a video game, actually) on Windows 8.1. 我有一个程序可以在Windows 8.1上打开一个窗口(实际上是一个视频游戏)。 That program then calls an "extension" in the form of a C++ .DLL that I compiled. 然后,该程序以我编译的C ++ .DLL的形式调用“扩展名”。 Within that DLL, I need to get a handle to the window of the program (the video game) that is calling the DLL. 在该DLL中,我需要获取正在调用DLL的程序(视频游戏)窗口的句柄。 I can do this using the FindWindow command combined with the name of the video game window. 我可以使用FindWindow命令结合视频游戏窗口的名称来执行此操作。 However, I sometimes need to have 2 copies of it open at once, both with the same window name. 但是,有时我需要一次打开两个副本,并且两个副本都具有相同的窗口名称。 This means that using FindWindow(windowName) is not guaranteed to select the window that I actually want. 这意味着使用FindWindow(windowName)不能保证选择我实际想要的窗口。 Is there a way to get a handle to the window that is the same application that is calling the C++ code, without having to specify the name? 有没有一种方法可以在不指定名称的情况下获取与正在调用C ++代码的应用程序相同的窗口的句柄?

对于从EnumWindow获得的与windowName相匹配的每个窗口,您可以检查HWND's进程并选择属于您正在运行的进程的窗口。可以使用GetWindowThreadProcessId函数来完成-它会为您提供该窗口所属进程的PID ,您可以将其与GetCurrentProcessId中的PID进行比较。

You can combine FindWindowEx to enumerate all the windows with the given name (set hwndParent to NULL to use desktop as the parent and just pass the previous result as the hwndChildAfter when you search the second time, and so on) with GetWindowThreadProcessId and GetCurrentProcessId to find out which of the windows belongs the the same thread you are being called from. 您可以结合使用FindWindowEx枚举具有给定名称的所有窗口(将hwndParentNULL以使用桌面作为父项,并在第二次搜索时将先前的结果作为hwndChildAfter传递,依此类推),并使用GetWindowThreadProcessIdGetCurrentProcessId进行查找哪个窗口属于您正在调用的同一线程

But then again - why not just pass the window handle to the dll directly? 但是再说一次-为什么不直接将窗口句柄传递给dll?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM