I'm trying to open a program within a panel in VB.Net Application. I can open up other exe's and such (like notepad), but it won't actually open the program I really want it to open within the panel specified. What it is doing is opening up that program outside of the application (like if you opened it from the start menu), not within the application. I really need some way to open this program within a panel on the canvas. Any help is appreciated!
VB.NET:
[/B]
[B]Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents m_clsAreaPanel As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.m_clsAreaPanel = New System.Windows.Forms.Panel
Me.SuspendLayout()
'
'm_clsAreaPanel
'
Me.m_clsAreaPanel.Location = New System.Drawing.Point(0, 0)
Me.m_clsAreaPanel.Name = "m_clsAreaPanel"
Me.m_clsAreaPanel.Size = New System.Drawing.Size(880, 528)
Me.m_clsAreaPanel.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(880, 533)
Me.Controls.Add(Me.m_clsAreaPanel)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private m_clsProcess As System.Diagnostics.Process = Nothing
Private Const GWL_STYLE As Integer = -16
Private Const WS_CAPTION As Integer = &HC00000
Private Const WS_BORDER As Integer = &H800000
Private Const SWP_FRAMECHANGED As Integer = &H20
Private Const SWP_NOMOVE As Integer = &H2
Private Const SWP_NOZORDER As Integer = &H4
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_SHOWWINDOW As Integer = &H40
Private Const SWP_NOACTIVATE As Integer = &H10
<System.Runtime.InteropServices.DllImport("user32", EntryPoint:="GetWindowLongA", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Shared Function GetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<System.Runtime.InteropServices.DllImport("user32", EntryPoint:="SetWindowLongA", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Shared Function SetWindowLong(ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
<System.Runtime.InteropServices.DllImport("user32", EntryPoint:="SetParent", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32", EntryPoint:="SetWindowPos", ExactSpelling:=True, CharSet:=System.Runtime.InteropServices.CharSet.Ansi, SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Long
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadProcess("C:\Program Files\Adver\start.exe")
End Sub
Public Sub LoadProcess(ByVal FileName As String)
' check for existing process...
If Not m_clsProcess Is Nothing Then
Throw New System.Exception("A process is already associated with this panel. Use ReleaseProcess first.")
End If
' set up process...
Dim clsProcessInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(FileName)
clsProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
' launch process...
m_clsProcess = System.Diagnostics.Process.Start(clsProcessInfo)
'Try
' m_clsProcess.WaitForInputIdle()
'Catch
'System.Threading.Thread.Sleep(50)
'End Try
' set parent of process to content area...
SetParent(m_clsProcess.MainWindowHandle, Me.m_clsAreaPanel.Handle)
' remove the caption and border...
SetWindowLong(m_clsProcess.MainWindowHandle, GWL_STYLE, GetWindowLong(m_clsProcess.MainWindowHandle, GWL_STYLE) And (Not WS_CAPTION) And (Not WS_BORDER))
' resize the window...
Me.ResizeWindow(True)
End Sub
Private Sub ResizeWindow(ByVal ShowWindow As Boolean)
' position the window...
Dim Flags As Integer = SWP_FRAMECHANGED Or SWP_NOZORDER
If ShowWindow Then
Flags = Flags Or SWP_SHOWWINDOW
Else
Flags = Flags Or SWP_NOACTIVATE
End If
SetWindowPos(m_clsProcess.MainWindowHandle, 0, 0, 0, Me.m_clsAreaPanel.Width, Me.m_clsAreaPanel.Height, Flags)
End Sub
Public Sub ReleaseProcess()
If Not m_clsProcess Is Nothing Then
m_clsProcess.CloseMainWindow()
m_clsProcess.Close()
m_clsProcess.Dispose()
m_clsProcess = Nothing
End If
End Sub
End Class
[/B]
[B]