释放 child window 回到桌面?

Release child window back to desktop?

我正在使用这些库来查找 window 并将其句柄设置为新句柄,就像我程序中的选项卡一样。但是,我很难将程序释放回桌面。一旦我关闭我的主应用程序,捕获的 window 也会关闭。有人可以帮我吗?谢谢!

图书馆:

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

以下是我如何将 运行 应用程序(例如记事本)捕获到我程序的活动选项卡中的方法:

SetParent(FindWindow(vbNullString, "Untitled - Notepad"), TabControl1.SelectedTab.Handle)

这在将 window 捕获到我的标签页时工作正常,但我如何将 window 从我的标签页删除回到桌面?

根据 the documentation:

SetParent function

Parameters

(...)

hWndNewParent [in, optional]

Type: HWND

A handle to the new parent window. If this parameter is NULL, the desktop window becomes the new parent window. (...)

所以您需要做的就是再次调用 SetParent() 并将第二个参数设置为 Nothing

'Class-level variable (so that you can reference the window later on again).
Dim NotepadHandle As IntPtr
'Adding it into your application.
NotepadHandle = FindWindow(Nothing, "Untitled - Notepad")
SetParent(NotepadHandle, TabControl1.SelectedTab.Handle)
'Removing it from your application.
SetParent(NotepadHandle, Nothing)

重要提示:谨慎使用!更改属于另一个进程(或只是另一个线程,甚至是您自己的应用程序中的线程)的 window 的父级可能会导致问题,尤其是当 window 从顶级 [=31] 移动时=](即,除了桌面之外没有父项的独立 window)到子项 window。

如果处理 window 的应用程序并非设计为支持此功能,它可能会导致各种问题,而且您永远无法真正确定会发生什么,因为这完全取决于应用程序的编码方式以及它可能决定或被指示做什么。

我推荐阅读 link that IInspectable 分享。它更详细地解释了情况,并有助于给出一个观点。