Results 1 to 4 of 4

Thread: SOLVED ! Run application inside a form

  1. #1

    Thread Starter
    Junior Member giannopo's Avatar
    Join Date
    Jan 2002
    Location
    Greece
    Posts
    27

    SOLVED ! Run application inside a form

    Hi,

    Is any way (API) to shell an application and to run it inside a form (window) using hwnd of this window?

    For exampe to run the world.exe inside my form1.

    Thanks in advance

    Elias
    Last edited by giannopo; Mar 17th, 2004 at 03:14 AM.
    elias

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Here is a quick sample for you:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow _
    4.     Lib "user32" Alias "FindWindowA" _
    5.     (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6.  
    7. Private Declare Function SetParent _
    8.     Lib "user32" (ByVal hWndChild As Long, _
    9.     ByVal hWndNewParent As Long) As Long
    10.  
    11. Private Declare Function ShellExecute _
    12.     Lib "shell32.dll" Alias "ShellExecuteA" _
    13.     (ByVal hwnd As Long, ByVal lpOperation As String, _
    14.     ByVal lpFile As String, ByVal lpParameters As String, _
    15.     ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    16.  
    17. Private Declare Function MoveWindow _
    18.     Lib "user32" (ByVal hwnd As Long, _
    19.     ByVal x As Long, ByVal y As Long, _
    20.     ByVal nWidth As Long, ByVal nHeight As Long, _
    21.     ByVal bRepaint As Long) As Long
    22.  
    23. Private Const SW_SHOWNORMAL = 1
    24.  
    25. Private Sub Command1_Click()
    26. Dim hw&
    27.  
    28.     ShellExecute Me.hwnd, vbNullString, "notepad.exe", vbNullString, "C:\", SW_SHOWNORMAL
    29.     hw = FindWindow(vbNullString, "Untitled - Notepad")
    30.     SetParent hw, Me.hwnd
    31.     MoveWindow hw, 0, 0, Me.ScaleWidth, Me.ScaleHeight, 1
    32.  
    33. End Sub

  3. #3
    Member
    Join Date
    Mar 2004
    Location
    Texas
    Posts
    53
    Just wanted to say thanks! This is very useful in what im working on.
    Good programming site:
    *http://www.planet-source-code.com

    Our CS Clan Page:
    *http://h2p.inter-gamer.com/index.html

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    2. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    3. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    6. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    7. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    8. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    9. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    10. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    11. Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    12. Const GW_HWNDNEXT = 2
    13. Dim mWnd As Long
    14. Function InstanceToWnd(ByVal target_pid As Long) As Long
    15.     Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    16.     'Find the first window
    17.     test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    18.     Do While test_hwnd <> 0
    19.         'Check if the window isn't a child
    20.         If GetParent(test_hwnd) = 0 Then
    21.             'Get the window's thread
    22.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
    23.             If test_pid = target_pid Then
    24.                 InstanceToWnd = test_hwnd
    25.                 Exit Do
    26.             End If
    27.         End If
    28.         'retrieve the next window
    29.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    30.     Loop
    31. End Function
    32. Private Sub Form_Load()
    33.     'KPD-Team 1999
    34.     'URL: [url]http://www.allapi.net/[/url]
    35.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    36.     Dim Pid As Long
    37.     'Lock the window update
    38.     LockWindowUpdate GetDesktopWindow
    39.     'Execute notepad.Exe
    40.     Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    41.     If Pid = 0 Then MsgBox "Error starting the app"
    42.     'retrieve the handle of the window
    43.     mWnd = InstanceToWnd(Pid)
    44.     'Set the notepad's parent
    45.     SetParent mWnd, Me.hwnd
    46.     'Put the focus on notepad
    47.     Putfocus mWnd
    48.     'Unlock windowupdate
    49.     LockWindowUpdate False
    50. End Sub
    51. Private Sub Form_Unload(Cancel As Integer)
    52.     'Unload notepad
    53.     DestroyWindow mWnd
    54.     'End this program
    55.     TerminateProcess GetCurrentProcess, 0
    56. End Sub


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width