Ok, now I need to embed/dock an excel spreadsheet started using the Process.Start method on a winform panel. I used the following code (Found on the internet), It workd only partially, as in, it hits and misses. Sometimes the excel spreadsheet is docked, while on other occassions excel opens not in the panel, but as a regular spreadsheet.
Following is the code I use:
[DllImport("user32.dll")]
private extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
DBConnect A = new DBConnect();
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
private Process pDocked;
private IntPtr hWndOriginalParent;
private IntPtr hWndDocked;
private 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 void dockIt(string path)
{
pDocked = new Process();
if (hWndDocked != IntPtr.Zero) //don't do anything if there's already a window docked.
{
// pDocked.WaitForInputIdle(1000);
//SetWindowPos(pDocked.MainWindowHandle, 0, 0, 0, 0, 0, 0X0040);
return;
}
hWndParent = IntPtr.Zero;
pDocked = Process.Start(@path);
while (hWndDocked == IntPtr.Zero)
{
//MessageBox.Show("RETURN");
pDocked.WaitForInputIdle(1000); //wait for the window to be ready for input;
pDocked.Refresh(); //update process info
if (pDocked.HasExited)
{
return; //abort if the process finished before we got a handle.
}
hWndDocked = pDocked.MainWindowHandle; //cache the window handle
}
//Windows API call to change the parent of the target window.
//It returns the hWnd of the window's parent prior to this call.
hWndOriginalParent = SetParent(hWndDocked, panel1.Handle);
//Wire up the event to keep the window sized to match the control
panel1.SizeChanged += new EventHandler(Panel1_Resize);
Panel1_Resize(new Object(), new EventArgs());
pDocked.WaitForInputIdle(1000);
SetWindowPos(pDocked.MainWindowHandle, 100, 100, 0, 0, 0, 0X0040|0X4);
}
private void undockIt()
{
//Restores the application to it's original parent.
SetParent(hWndDocked, hWndOriginalParent);
}
private void Panel1_Resize(object sender, EventArgs e)
{
//MessageBox.Show("resize");
//Change the docked windows size to match its parent's size.
MoveWindow(hWndDocked, 0, 0, panel1.Width, panel1.Height, true);
}