使用Win32 API验证HWND [英] Validate HWND using Win32 API
问题描述
使用C ++的本地Win32 API有一种方法可以确定与HWND关联的窗口是否仍然有效?
From the native Win32 API using C++ is there a way to determine whether the window associated with an HWND is still valid?
推荐答案
p>您可以使用Win32 API IsWindow 。
You could use the Win32 API IsWindow.
不推荐使用它虽然有两个原因:
It is not recommended to use it though for 2 reasons:
- Windows窗口句柄可以在窗口销毁后重复使用,所以你不知道你是否有一个完全不同的窗口的句柄。
- 在此通话后,州/省可能会直接更改,您认为该号码有效,但实际上无效。
从MSDN(与上面的链接相同):
From MSDN (same link as above):
线程不应使用IsWindow作为
窗口,它没有创建因为
窗口可以在
后被销毁此函数被调用。此外,
因为窗口句柄被回收
句柄甚至可以指向
不同的窗口。
A thread should not use IsWindow for a window that it did not create because the window could be destroyed after this function was called. Further, because window handles are recycled the handle could even point to a different window.
可以做什么?
也许您的问题可以重新架构,以便您不需要检查有效的处理。也许例如你可以建立从客户端到服务器的管道。
Perhaps your problem can be re-architected so that you do not have the need to check for a valid handle. Maybe for example you can establish a pipe from the client to the server.
您还可以创建一个Windows钩子来检测某些消息是否发生,但这对大多数需求来说可能是过度的。
You could also create a windows hook to detect when certain messages occur, but this is probably overkill for most needs.
这篇关于使用Win32 API验证HWND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!