|
Running External Applications in a Windows Form
Published: 4/5/2008 |
|
From time to time over at the VBCity forums the topic of running an external program in the context of a WinForms application comes up. What people want to do is have an external program running in a control on one of the forms on their program. This is possible to do with a little P/Invoke interop code, but it doesn't work with every external application. Some programs are written such that they simply won't allow themselves to run in the context of another application. With others, you might run into some weird results, so use this code with care:
VB:
Public Class ShellPanel
Inherits System.Windows.Forms.Panel
Private m_clsAreaPanel As System.Windows.Forms.Panel
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
Public Sub New()
m_clsAreaPanel = New System.Windows.Forms.Panel()
m_clsAreaPanel.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.ContentArea.Controls.Add(m_clsAreaPanel)
m_clsAreaPanel.Dock = System.Windows.Forms.DockStyle.Fill
End Sub
Protected Overrides Sub Finalize()
Me.ReleaseProcess()
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.Minimized
' 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
pipe
SWP_NOZORDER
If ShowWindow Then
Flags = Flags
pipe
SWP_SHOWWINDOW
Else
Flags = Flags
pipe
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
Protected Overrides Sub OnResize(ByVal e As EventArgs)
MyBase.OnResize(e)
If Not m_clsProcess Is Nothing Then
Me.ResizeWindow(False)
End If
End Sub
End Class
public class ShellPanel : System.Windows.Forms.Panel
{
private System.Windows.Forms.Panel m_clsAreaPanel;
private System.Diagnostics.Process m_clsProcess = null;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0XC00000;
private const int WS_BORDER = 0X800000;
private const int SWP_FRAMECHANGED = 0X20;
private const int SWP_NOMOVE = 0X2;
private const int SWP_NOZORDER = 0X4;
private const int SWP_NOSIZE = 0X1;
private const int SWP_SHOWWINDOW = 0X40;
private const int SWP_NOACTIVATE = 0X10;
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowLongA", ExactSpelling = true]
[CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowLongA", ExactSpelling = true]
[CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetParent", ExactSpelling = true]
[CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetWindowPos", ExactSpelling = true]
[CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern long SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int wFlags);
public ShellPanel()
{
m_clsAreaPanel = new System.Windows.Forms.Panel();
m_clsAreaPanel.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.ContentArea.Controls.Add(m_clsAreaPanel);
m_clsAreaPanel.Dock = System.Windows.Forms.DockStyle.Fill;
}
~ShellPanel()
{
this.ReleaseProcess();
}
public void LoadProcess(string FileName)
{
// check for existing process...
if (m_clsProcess != null) { throw new System.Exception("A process is already associated with this panel. Use ReleaseProcess first."); }
// set up process...
System.Diagnostics.ProcessStartInfo clsProcessInfo = new System.Diagnostics.ProcessStartInfo(FileName);
clsProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
// launch process...
m_clsProcess = System.Diagnostics.Process.Start(clsProcessInfo);
try
{
m_clsProcess.WaitForInputIdle();
}
catch
{
System.Threading.Thread.Sleep(50);
}
// set parent of process to content area...
SetParent(m_clsProcess.MainWindowHandle, this.m_clsAreaPanel.Handle);
// remove the caption and border...
SetWindowLong(m_clsProcess.MainWindowHandle, GWL_STYLE,
GetWindowLong(m_clsProcess.MainWindowHandle, GWL_STYLE) & ~WS_CAPTION & ~WS_BORDER);
// resize the window...
this.ResizeWindow(true);
}
private void ResizeWindow(bool ShowWindow)
{
// position the window...
int Flags = SWP_FRAMECHANGED | SWP_NOZORDER;
if (ShowWindow) { Flags = Flags | SWP_SHOWWINDOW; } else { Flags = Flags | SWP_NOACTIVATE; }
SetWindowPos(m_clsProcess.MainWindowHandle, 0, 0, 0, this.m_clsAreaPanel.Width, this.m_clsAreaPanel.Height, Flags);
}
public void ReleaseProcess()
{
if (m_clsProcess != null)
{
m_clsProcess.CloseMainWindow(); m_clsProcess.Close(); m_clsProcess.Dispose(); m_clsProcess = null;
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e); if (m_clsProcess != null) { this.ResizeWindow(false); }
}
}
Questions or Comments? .
VB to C# and C# to VB translation provided by Instant C# and Instant VB.