ProcessHosting

Contenu du snippet

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Drawing


Class Processhost


    ' Inherits Form
    Private p As Process


    <DllImport("user32.dll", SetLastError:=True)> _
    Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function


    <DllImport("user32.dll")> _
    Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function



    Private Shared ReadOnly HWND_TOP As New IntPtr(0)
    Public Const SWP_NOACTIVATE As Integer = &H10
    Public Const SWP_SHOWWINDOW As Integer = &H40


    'process name may include a path and filename
    'container handle may be me.handle or similar
    'rect the rectangle where you wish to see the process
    '
    'example:
    'WBool= Start£Process("mspaint.exe",panel1.handle,Me.ClientRectangle)
    '
    Public Function StartProcess(ByVal ProcessName As String, ByVal ContainerHandle As IntPtr, ByVal Rect As Rectangle) As Boolean
        Try
            p = New Process()
            p.StartInfo.FileName = ProcessName
            p.Start()
            p.WaitForInputIdle(-1)
            SetParent(p.MainWindowHandle, ContainerHandle)
            SetWindowPos(p.MainWindowHandle, HWND_TOP, Rect.Left, Rect.Top, Rect.Width, Rect.Height, SWP_NOACTIVATE Or SWP_SHOWWINDOW)


        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function


    ' call this in your form resize :)
    Public Function ParentResize(ByVal rect As Rectangle) As Boolean


        Try
            SetWindowPos(p.MainWindowHandle, HWND_TOP, rect.Left, rect.Top, rect.Width, rect.Height, SWP_NOACTIVATE Or SWP_SHOWWINDOW)


        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function


End Class


Compatibilité : VB 2005, VB 2008

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.