C++ 如何检测 HWND 是否属于我的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35054617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:28:09  来源:igfitidea点击:

How to detect if a HWND belong to my application?

c++winapi

提问by TheBlueNotebook

My application creates multiple windows and I want to enforce a Z order in-between these windows, i.e, if two windows happen to overlap, a window with a higher priority should be painted over the window with lower priority.

我的应用程序创建了多个窗口,我想在这些窗口之间强制执行 Z 顺序,即,如果两个窗口碰巧重叠,则应将优先级较高的窗口绘制在优先级较低的窗口上。

I am getting the handles of different (invisible/visible) windows wrt to a window using the GetWindowfunction. I want to know if a particular window returned by this function is created by my application. Any ideas on how can I get that information?

我正在使用GetWindow函数将不同(不可见/可见)窗口的句柄写入窗口。我想知道此函数返回的特定窗口是否由我的应用程序创建。关于如何获取该信息的任何想法?

回答by CristiFati

You can use [MS.Docs]: GetWindowThreadProcessId function.

您可以使用[MS.Docs]: GetWindowThreadProcessId 函数

In order to check the current process id, use [MS.Docs]: GetCurrentProcessId function.

为了检查当前进程 ID,请使用[MS.Docs]: GetCurrentProcessId 函数

If your app is more complex and spawns multiple processes that create windows, you'll have to construct the process tree using [MS.Docs]: Tool Help Functions((CreateToolhelp32Snapshot, Process32First, Process32Next) + CloseHandle).

如果您的应用程序更复杂并产生多个创建窗口的进程,则必须使用[MS.Docs]构建进程树:工具帮助函数((CreateToolhelp32Snapshot, Process32First, Process32Next)+ CloseHandle)。