使用其他应用程序从其父窗口中分离子窗口

时间:2015-06-12 14:53:27

标签: vb.net winapi

我是visual basic的新手。我正在尝试构建一个可以控制我的其他应用程序的应用程序。

所以我的第一个应用程序在主窗口中有一个子窗口。

我希望我的第二个应用程序能够控制我的第一个应用程序。通过按下我的第二个应用程序上的按钮,它应该使第一个应用程序中的子窗口成为一个单独的窗口。该窗口可以自由移动到其原始父窗口之外。它应该作为一个单独的窗口。即使原始父窗口最小化,子窗口也会保持运行状态。

我很乐意让它发挥作用,但并不像预期的那样。

The problems I encounter now are:

     1. After the child window is detached, the child window still reminds in a background 
        frame looks like the background of its original parent window. I want the window to
        be on the desktop.
     2. After the child window is detached, the child window minimize with its main window. 
        I want the child window stay even if I minimize the main window. 

以下是我的程序代码:

基本上它试图通过窗口标题获取主窗口的句柄,然后获取其子窗口的句柄并使用SetParent函数将其父窗口更改为桌面,因此它将直接显示在桌面上。

我知道可能有其他方法可以做到这一点,请告诉我。如果你具体,那将是非常有帮助的,因为我提到我是visual basic的新手。我的代码可能看起来很乱,抱歉。 :P谢谢大家!

Option Explicit On

Public Class FormG
    Const GW_CHILD = 5
    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim r As Int32, hChild As Long, desk As Long
        r = FindWindow(vbNullString, "Form1")
        hChild = GetWindow(r, GW_CHILD)
        desk = GetDesktopWindow()
        SetParent(hChild, desk)
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

您不应该尝试将孩子重新附加到另一个窗口。当您可以致电SetParentit will be very, very hard正确行事并解决所有问题。如果您被告知某人现在是您的父母,那就好了。所有参与其中的人是否真的表现得好像是真的?

问题的解决方案是让第一个应用程序有两个窗口共享相同的代码:子窗口和非子窗口。你只需隐藏你不需要的那个。

然而,我有一种感觉,你正在努力实现一些更直接,更简单的解决方案。您应该描述通过提取窗口尝试解决的问题。