I am using these libraries to find a window and set it's handle to a new handle, like a tab in my program. However, I am having a difficult time releasing the program back to the desktop. Once I close my main application, the window that was captured also closes. Could someone give me a hand please? thank you!
The library :
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
Here is how I would capture a running application, like notepad for example into the active tab in my program :
SetParent(FindWindow(vbNullString, "Untitled - Notepad"), TabControl1.SelectedTab.Handle)
This works fine when capturing the window into my tabpage, but how would I remove that window from my tabpage back onto the desktop?
Per the documentation:
So all you need to do is call
SetParent()again with the second parameter set toNothing.IMPORTANT: Use this with caution! Changing the parent of a window belonging to another process (or simply another thread, even one in your own application) may cause issues, especially when that window is moved from being a top-level window (that is, a standalone window without a parent other than the desktop) to a child window.
If the application handling the window wasn't designed to support this it can cause all kinds of problems, and you can never really be certain what might happen because it all depends on how the application was coded and what it might decide or be instructed to do.
I recommend reading the link that IInspectable shared. It explains the situation a little more in detail and helps give a perspective on it.