Open external application inside form vb.net

0

I have a doubt, I wanted to load an external exe (example: c: \ dark.exe) inside a picture box the size of it, I tried the code below but it does not work (obs the form needs to work in the folder of destination). and even if dark.exe is open, when clicking the button the form pulls it in

 Public Class Form1
        Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Private Const WM_SYSCOMMAND As Integer = 274
        Private Const SC_MAXIMIZE As Integer = 61488
        Dim proc As Process

        Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

            proc = Process.Start("C:\WINDOWS\notepad.exe")
            proc.WaitForInputIdle()
            SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
            SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            Me.BringToFront()
        End Sub
    End Class
    
asked by anonymous 30.04.2016 / 06:31

1 answer

0

There's no way you can load an executable to run within your form or any of the controls - and that, if there was a way, I'd use TANTA API that I fear is not possible in .NET

The maximum you can do is try to open the executable in a window and try to reposition this window in the same coordinates as your application or Picture.

Use "SetWindowPos" as described here: link

Here you can have an example in C #: link

or something like this:

  Imports System.Collections.Generic
  Imports System.Linq
  Imports System.Text
  Imports System.Diagnostics
  Imports System.Runtime.InteropServices
  Imports System.Threading

  Class Program
    Private Shared Sub Main(args As String())

        Dim flash As New Process()
        flash.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        flash.StartInfo.FileName = "D:\FlashPlayer.exe"
        flash.Start()
        Thread.Sleep(100)

        Dim id As IntPtr = flash.MainWindowHandle
        Console.Write(id)
        Program.MoveWindow(flash.MainWindowHandle, 0, 0, 500, 500, True)
    End Sub

<DllImport("user32.dll", SetLastError := True)> _
Friend Shared Function MoveWindow(hWnd As IntPtr, X As Integer, Y As Integer, nWidth As Integer, nHeight As Integer, bRepaint As Boolean) As Boolean
End Function


   End Class
    
04.05.2016 / 01:22