#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPISysWin.au3>
Global $hEmbed
$hGUI = GUICreate('ProcessWinEmbed Example', 800, 600)
$iButton = GUICtrlCreateButton('Embed', 20, 20, 70, 20)
GUISetState(@SW_SHOW, $hGUI)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
GUISetState(@SW_HIDE, $hGUI)
If $hEmbed Then
WinClose($hEmbed)
EndIf
Exit
Case $iButton
If $hEmbed Then
GUICtrlSetData($iButton, 'Embed')
WinClose($hEmbed)
$hEmbed = 0
Else
GUICtrlSetData($iButton, 'Remove')
$hEmbed = _ProcessWinEmbed("notepad.exe", $hGUI, 0, 50)
EndIf
EndSwitch
WEnd
Func _ProcessWinEmbed($sProgram, $hParent, $iLeft = -1, $iTop = -1)
Local $iPID = Run($sProgram, '', @SW_HIDE)
Local $aWinList, $hWnd
Local $iTimer = TimerInit()
While TimerDiff($iTimer) < 3000
$aWinList = WinList()
For $i = 1 To $aWinList[0][0]
If Not BitAND(WinGetState($aWinList[$i][1]), 2) And WinGetProcess($aWinList[$i][1]) = $iPID Then
$hWnd = $aWinList[$i][1]
ExitLoop 2
EndIf
Next
WEnd
If Not IsHWnd($hWnd) Then
Return SetError(1, 0, 0)
EndIf
Local $iStyle = _WinAPI_GetWindowLong($hParent, $GWL_STYLE)
_WinAPI_SetWindowLong($hParent, $GWL_STYLE, BitOR($iStyle, $WS_CLIPCHILDREN))
Local $aWinPos = WinGetClientSize($hParent)
Local $iExStyle = _WinAPI_GetWindowLong($hWnd, $GWL_EXSTYLE)
_WinAPI_SetWindowLong($hWnd, $GWL_EXSTYLE, BitOr($iExStyle, $WS_EX_MDICHILD))
_WinAPI_SetWindowLong($hWnd, $GWL_STYLE, $WS_POPUP)
_WinAPI_SetWindowPos($hWnd, 0, 0, 0, 0, 0, BitOR($SWP_SHOWWINDOW, $SWP_NOSIZE, $SWP_NOMOVE, $SWP_NOZORDER))
_WinAPI_SetParent($hWnd, $hParent)
WinMove($hWnd, '', ($iLeft = -1 ? 2.5 : $iLeft), ($iTop = -1 ? 2.5 : $iTop), $aWinPos[0] - ($iLeft = -1 ? 5 : $iLeft), $aWinPos[1] - ($iTop = -1 ? 5 : $iTop))
WinSetState($hWnd, '', @SW_SHOW)
Return $hWnd
EndFunc