vb.net中我也用SetParent API做过 部分代码如下Public Class frmWindowsManage Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32 Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32 Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32 Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32 Private Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) As IntPtr Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean Private Const GWL_HWNDPARENT As Int32 = -8 Private newWindowList As List(Of String) Private newHandleList As List(Of IntPtr) Private Structure RECT Public left As Integer Public top As Integer Public right As Integer Public bottom As Integer Public Sub New(ByVal _left As Integer, ByVal _top As Integer, ByVal _right As Integer, ByVal _bottom As Integer) left = _left top = _top right = _right bottom = _bottom End Sub End Structure Private Function EnumWinProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean If IsWindowVisible(hwnd) Then If GetParent(hwnd) = IntPtr.Zero Then If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then Dim str As String = String.Empty.PadLeft(GetWindowTextLength(hwnd) + 1) Call GetWindowText(hwnd, str, str.Length) If Not String.IsNullOrEmpty(str.Substring(0, str.Length - 1)) Then newWindowList.Add(str.Substring(0, str.Length - 1)) newHandleList.Add(hwnd) End If End If End If End If EnumWinProc = True End Function Private Sub RefreshWindowList() newWindowList = New List(Of String) newHandleList = New List(Of IntPtr) Call EnumWindows(AddressOf EnumWinProc, CInt(True)) End Sub Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click Call RefreshWindowList() Me.lstWindows.Items.Clear() For Each item As String In newWindowList Me.lstWindows.Items.Add(item) Next End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, lstWindows.DoubleClick For Each title As Object In Me.lstWindows.SelectedItems Dim frm As New Form() frm.AutoScroll = True Call SetParent(Me.newHandleList(Me.lstWindows.Items.IndexOf(title)), frm.Handle) Next End Sub
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process _Process = System.Diagnostics.Process.Start(@"C:\WINDOWS\notepad.exe"); while (!_Process.WaitForInputIdle())
{
Application.DoEvents();
} SetParent(_Process.MainWindowHandle, this.Handle);
}
匹配ID 然后再使用SetParent
部分代码如下Public Class frmWindowsManage Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
Private Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32
Private Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) As IntPtr
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean Private Const GWL_HWNDPARENT As Int32 = -8
Private newWindowList As List(Of String)
Private newHandleList As List(Of IntPtr) Private Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
Public Sub New(ByVal _left As Integer, ByVal _top As Integer, ByVal _right As Integer, ByVal _bottom As Integer)
left = _left
top = _top
right = _right
bottom = _bottom
End Sub
End Structure Private Function EnumWinProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = IntPtr.Zero Then
If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then
Dim str As String = String.Empty.PadLeft(GetWindowTextLength(hwnd) + 1)
Call GetWindowText(hwnd, str, str.Length)
If Not String.IsNullOrEmpty(str.Substring(0, str.Length - 1)) Then
newWindowList.Add(str.Substring(0, str.Length - 1))
newHandleList.Add(hwnd)
End If
End If
End If
End If
EnumWinProc = True
End Function Private Sub RefreshWindowList()
newWindowList = New List(Of String)
newHandleList = New List(Of IntPtr)
Call EnumWindows(AddressOf EnumWinProc, CInt(True))
End Sub Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
Call RefreshWindowList()
Me.lstWindows.Items.Clear()
For Each item As String In newWindowList
Me.lstWindows.Items.Add(item)
Next
End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, lstWindows.DoubleClick
For Each title As Object In Me.lstWindows.SelectedItems
Dim frm As New Form()
frm.AutoScroll = True
Call SetParent(Me.newHandleList(Me.lstWindows.Items.IndexOf(title)), frm.Handle)
Next
End Sub
End Class