run .exe file in a panel
-
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
-
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
Using the
SetParent
API might help you achieve this.[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);void LoadProcessInPanel
{
...
Process process = Process.Start("calc.exe");
SetParent(process.MainWindowHandle, myPanel.Handle);
...
} -
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
The solution Abhinav gave you will not work as is: you need to wait for the window to be fully loaded before you set the new value:
Process process = Process.Start("calc.exe"); Thread.Sleep(500); SetParent(process.MainWindowHandle, myPanel.Handle);
You will also need to move it to a "sensible" location within your panel after you display it.
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const short SWP_NOMOVE = 0X2;
private const short SWP_NOSIZE = 1;
private const short SWP_NOZORDER = 0X4;
private const int SWP_SHOWWINDOW = 0x0040;private void myButton_Click(object sender, EventArgs e)
{
Process process = Process.Start("calc.exe");
Thread.Sleep(500);
IntPtr processHandle = process.MainWindowHandle;
SetParent(processHandle, myPanel.Handle);
SetWindowPos(processHandle, 0, 0, 0, myPanel.Bounds.Width, myPanel.Bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
}You might want to look at this: Window Tabifier[^] - it's a bit more complex that you appear to need, but it does everything you want to!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Using the
SetParent
API might help you achieve this.[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);void LoadProcessInPanel
{
...
Process process = Process.Start("calc.exe");
SetParent(process.MainWindowHandle, myPanel.Handle);
...
}Unless Calc.exe starts a lot quicker than it does on my PC, you need a delay before you change the parent - it won't work until the window is fully constructed. :laugh:
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
I've just been playing around with this as your question interested me. There's a bit more to just setting the parent handle. This is what I came up with after a little research and testing, it will still need further work but it's a start:
using System;
using System.Runtime.InteropServices;internal static class NativeMethods
{
public const int GWL_STYLE = -16;
public const int WS_VISIBLE = 0x10000000;// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633530(v=vs.85).aspx /\*
BOOL WINAPI IsWindowVisible(
__in HWND hWnd
);
*/
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633534(v=vs.85).aspx /\*
BOOL WINAPI MoveWindow(
__in HWND hWnd,
__in int X,
__in int Y,
__in int nWidth,
__in int nHeight,
__in BOOL bRepaint
);
*/
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633541(v=vs.85).aspx /\*
HWND WINAPI SetParent(
__in HWND hWndChild,
__in_opt HWND hWndNewParent
);
*/
[DllImport("User32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);// http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx /\*
LONG WINAPI SetWindowLong(
__in HWND hWnd,
__in int nIndex,
__in LONG dwNewLong
);
*/
[DllImport("User32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;internal class ProcessWrapper
{
private Process process = null;public ProcessWrapper(Process process) { this.process = process; } public void Close() { if (process != null) process.CloseMainWindow(); } public void PlaceInControl(Control control) { if (control != null && process != null) { while ( process.MainWindowHandle == IntPtr.Zero || !NativeMethods.IsWindowVisible(process.MainWindowHandle)) { process.Refresh(); } process.WaitForInputIdle();
-
The solution Abhinav gave you will not work as is: you need to wait for the window to be fully loaded before you set the new value:
Process process = Process.Start("calc.exe"); Thread.Sleep(500); SetParent(process.MainWindowHandle, myPanel.Handle);
You will also need to move it to a "sensible" location within your panel after you display it.
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const short SWP_NOMOVE = 0X2;
private const short SWP_NOSIZE = 1;
private const short SWP_NOZORDER = 0X4;
private const int SWP_SHOWWINDOW = 0x0040;private void myButton_Click(object sender, EventArgs e)
{
Process process = Process.Start("calc.exe");
Thread.Sleep(500);
IntPtr processHandle = process.MainWindowHandle;
SetParent(processHandle, myPanel.Handle);
SetWindowPos(processHandle, 0, 0, 0, myPanel.Bounds.Width, myPanel.Bounds.Height, SWP_NOZORDER | SWP_SHOWWINDOW);
}You might want to look at this: Window Tabifier[^] - it's a bit more complex that you appear to need, but it does everything you want to!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
You can use the IsWindowVisible[^] function with a while loop instead of sleeping the thread
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
You can use the IsWindowVisible[^] function with a while loop instead of sleeping the thread
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn):doh: I need another coffee...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
-
How to place a .exe file into the panel in visual studio windows form application c#. using this
System.Diagnostics.Process.Start("calc.exe");
the calculator.exe will pop up after a button is being clicked. how do i do it in a way such that the calculator is being displayed in the panel after the button is clicked? and also if i want to run another exe file of my own in this panel how do i go about doing it?
i have tried out and it works! but right now i am facing another problem! how do i adjust the panel width and height? I fixed my panel on the top left hand side with a width of 400px and height 400px. however after the button is being clicked, the exe appear but the position of the panel shifted. how do i ensure that the panel stays where it was as of before? and also before the exe file is being displayed in the panel, it will pop up the exe first. how can i do it in such a way that it wont pop up and will be displayed immediately in the panel?