I'm trying to hosting an exe(Putty) on the WPF program by using WinfowsFormsHost
and success, here is the code:
[DllImport("user32")]
static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
//Putty.exe has been start by cmdline and here is the `process`
public void AddPuttyPage(Process process)
{
var picPan = new System.Windows.Forms.Panel();
var winform = new WindowsFormsHost
{
Child = picPan
};
//Tabs are bind to the TabControl on a xaml
Tabs.Add(new TabItem { Header = "Tab1", Content = winform });
process.WaitForInputIdle();
SetParent(process.MainWindowHandle, picPan.Handle);
}
The Putty.exe has been embed on the WPF tabItem successful,But there still have two problem to sovle.
To solve first question,I use GetWindowLong
and SetWindowLong
,and to solve second question ,I use SetWindowPos
,Here is the code:
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("USER32.DLL")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
const int GWL_STYLE = -16;
const int WS_CHILD = 0x40000000;
const int WS_CAPTION = 0x00C00000;
const int WS_SYSMENU = 0x00080000;
const int WS_THICKFRAME = 0x00040000;
const int WS_MINIMIZE = 0x20000000;
const int WS_MAXIMIZEBOX = 0x00010000;
const int SWP_NOZORDER = 0x0004;
const int SWP_NOACTIVATE = 0x0010;
public void AddPuttyPage(Process process){
//code upside
....
int Style = GetWindowLong(process.MainWindowHandle, GWL_STYLE);
Style &= ~WS_CAPTION;
Style &= ~WS_SYSMENU;
Style &= ~WS_THICKFRAME;
Style &= ~WS_MINIMIZE;
Style &= ~WS_MAXIMIZEBOX;
SetWindowLong(process.MainWindowHandle, GWL_STYLE, Style | WS_CHILD);
SetWindowPos(process.MainWindowHandle, process.MainWindowHandle.Zero, 0, 0, picPan.Width, picPan.Height, SWP_NOZORDER | SWP_NOACTIVATE);
}
But It doesn't work well.
For the first question,The button on the title bar is removed ,but the title and the border are still here,and I can't focus on the putty window by click it and can't input anything by keyboard.Anything wrong of this solution?
For the second question,the default size of the System.Windows.Forms.Panel
is 100*200,I don't know how to get the size of the tabWindow to make the putty.exe fill it.
User contributions licensed under CC BY-SA 3.0