Open external application within form Vb.net

Asked

Viewed 1,317 times

0

i have a doubt, I wanted to load an external exe(example: c: dark.exe) inside a picture box the size of the same, I tried the code below but it doesn’t work( Obs the form needs to work in the target folder). and even if the dark.exe is open, clicking the form button 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

1 answer

0

There’s no way you can load an executable to run inside your form or any of the controls - and that, if there was a way, would use SO MUCH API that I’m afraid it’s not possible in. NET

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

Use "Setwindowpos" as described here: https://msdn.microsoft.com/en-us/library/ms633545(VS.85). aspx

Here Voce can have an example in C#: http://www.pinvoke.net/default.aspx/user32.setwindowpos

or something as follows:

  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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.