windows 在 C 中为我自己的应用程序获取 HWND
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1125564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting the HWND for my own application in C
提问by wonderer
since I couldn't find an answer to thisquestion I researched a bit further into the MSDN and I found isChild(). It might give me the answer to that other question.
因为我找不到这个问题的答案,所以我进一步研究了 MSDN,发现 isChild()。它可能会给我另一个问题的答案。
Now, in order to use isChild() I need to pass the HWND of the parent application that I want to check, in this case my own application. How do I get the HWND of my own application?
现在,为了使用 isChild(),我需要传递我想要检查的父应用程序的 HWND,在这种情况下是我自己的应用程序。我如何获得我自己的应用程序的 HWND?
I don't know the title since it changes constantly so I can't use FindWindow().
我不知道标题,因为它不断变化,所以我不能使用 FindWindow()。
Thanks
谢谢
Edit:
编辑:
Since it's not clear, I'll add more information: I am not creating a window. I don't have access to the creation of a window. My code is a piece of code that gets compiled together with whatever application the other programmer is coding and I have no access to how the window is created, the title or any other information. So, how do I get the HWND to the "WINDOW" of the application I am running?
由于不清楚,我将添加更多信息:我没有创建窗口。我无权创建窗口。我的代码是一段代码,它与其他程序员正在编码的任何应用程序一起编译,我无法访问窗口的创建方式、标题或任何其他信息。那么,如何将 HWND 获取到我正在运行的应用程序的“WINDOW”?
采纳答案by Mikhail Churbanov
Use GetTopWindow() and GetNextWindow() to walk through windows z-order.
使用 GetTopWindow() 和 GetNextWindow() 遍历窗口 z 顺序。
However, don't think it is necessary, but you can use GetCurrentProcessId() and GetWindowThreadProcessId(), may be something like following will help you:
但是,不要认为这是必要的,但是您可以使用 GetCurrentProcessId() 和 GetWindowThreadProcessId(),以下内容可能会对您有所帮助:
HWND FindMyTopMostWindow()
{
DWORD dwProcID = GetCurrentProcessId();
HWND hWnd = GetTopWindow(GetDesktopWindow());
while(hWnd)
{
DWORD dwWndProcID = 0;
GetWindowThreadProcessId(hWnd, &dwWndProcID);
if(dwWndProcID == dwProcID)
return hWnd;
hWnd = GetNextWindow(hWnd, GW_HWNDNEXT);
}
return NULL;
}
回答by jalf
Your application doesn't have a HWND. The window does. An application may have no windows or it may have many, so there is no general function to "Get the application's HWND".
您的应用程序没有 HWND。窗户可以。应用程序可能没有窗口,也可能有很多窗口,因此没有“获取应用程序的 HWND”的通用功能。
The obvious solution is just to hold on to the handle when you get it. When you create the window, a HWND is returned. Store that.
显而易见的解决方案是在你拿到把手时抓住它。创建窗口时,会返回 HWND。存储那个。
回答by Martin B
As others have already pointed out
正如其他人已经指出的那样
- In general, an application can have zero or multiple top-level windows.
- If you're creating the window yourself you can just remember the HWND somewhere.
- 通常,一个应用程序可以有零个或多个顶级窗口。
- 如果您自己创建窗口,您可以在某处记住 HWND。
But maybe your code is in a DLL, so you didn't actually create the top-level window yourself. So what to do?
但也许您的代码在 DLL 中,因此您实际上并没有自己创建顶级窗口。那么该怎么办?
I would suggest the following:
我建议如下:
- Use
EnumWindows
to enumerate all top-level windows. - Use
GetWindowLongPtr
to get the HINSTANCE for each top-level window. Compare this against the HINSTANCE of the application, which you can get usingGetModuleHandle(NULL)
. If they're identical, you've found your main window.
- 使用
EnumWindows
枚举所有顶级窗口。 - 使用
GetWindowLongPtr
以获得每个顶层窗口的HINSTANCE。将此与应用程序的 HINSTANCE 进行比较,您可以使用GetModuleHandle(NULL)
. 如果它们相同,则您已找到主窗口。
Edit: Here is some code. Turns out you also have to use IsWindowVisible
because there seem to be quite a few invisible "helper" windows.
编辑:这是一些代码。原来你也必须使用,IsWindowVisible
因为似乎有很多不可见的“帮助”窗口。
HWND hwndMain;
BOOL CALLBACK EnumWindowProc(HWND hwnd, LPARAM lParam)
{
HINSTANCE hinst=(HINSTANCE)GetModuleHandle(NULL);
if((HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE)==hinst &&
IsWindowVisible(hwnd))
{
hwndMain=hwnd;
return FALSE;
}
else
return TRUE;
}
Then in the place you want to find the window:
然后在你想找到窗口的地方:
hwndMain=NULL;
EnumWindows(EnumWindowProc, 0);
And after this, hwndMain
should contain the handle of the window, or NULL
if none exists.
在此之后,hwndMain
应该包含窗口的句柄,或者NULL
如果不存在。
Using EnumWindows
is a bit burdensome but is recommended over calling GetWindow
in a loop because, as MSDN notes: "An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed."
使用EnumWindows
有点麻烦,但建议GetWindow
在循环中调用,因为,正如 MSDN 所指出的:“调用 GetWindow 来执行此任务的应用程序可能会陷入无限循环或引用已被破坏的窗口的句柄。”
回答by Martin B
Presumably your code gets called by main application code, otherwise what use is it? In which case I acnnot see why your code's API cannot include some way of informing you of the handle of the application's main window.
大概你的代码被主应用程序代码调用了,否则有什么用?在这种情况下,我不明白为什么您的代码的 API 不能包含某种方式来通知您应用程序主窗口的句柄。
回答by Alynuzzu
You can inject a DLL in a thread who appeal user32.dll http://msdn.microsoft.com/en-us/library/ms821625.aspx
您可以在吸引 user32.dll 的线程中注入 DLL http://msdn.microsoft.com/en-us/library/ms821625.aspx
回答by Goz
Can't you just hold onto the handle returned from CreateWindow? If not, why not?
你不能抓住从 CreateWindow 返回的句柄吗?如果没有,为什么不呢?
回答by rslite
This is old for me, but IIRC you should receive the HWND as a parameter in the window proc. You can save it in a global variable somewhere in the beginning.
这对我来说很旧,但是 IIRC 您应该在窗口 proc 中接收 HWND 作为参数。您可以在开始时将其保存在全局变量中。
回答by ba__friend
What about your windows class name? Is it always different on window creation? If not you could still you FindWindow().
你的windows类名怎么样?窗口创建总是不同的吗?如果没有,您仍然可以使用 FindWindow()。