问题
Is it possible for 2 windows managed by Windows to have the same window handle value?
Consider both cases when these 2 windows are opened by the same process and when these 2 windows are opened by 2 different processes.
回答1:
Quick answer: no, it's not possible if they are inside the same desktop, every window handle issued by the window manager is unique (inside its desktop) throughout all the existence of the window, otherwise it couldn't uniquely identify such window on the system current window station desktop (which is defined by MSDN as "a logical display surface" that "contains user interface objects such as windows, menus, and hooks; it can be used to create and manage windows").
Long answer:
The issue is discussed here, which points to this article. It seems that HWNDs are local to desktops , so you can have two identical HWNDs that point to two different windows, but they are necessarily in two separated desktops, so there's no ambiguity (the WM handle you pass to every user32 function refers implicitly to the current desktop, to go to another one you have to use SetThreadDesktop, and this call fails if your thread owns any desktop-contained object into the current desktop).
On the other hand, when a window is destroyed, its handle can be reused, but IIRC the window manager employs some techniques to avoid immediately reusing a handle.
回答2:
Every window open at a particular time will have a unique handle. However, they can be reused once a window is closed.
来源:https://stackoverflow.com/questions/7308887/is-it-possible-for-2-windows-managed-by-windows-to-have-the-same-window-handle-v