Reputation: 247
I'm working in Visual Studio 2015, in C # and WPF technology. I need to embed to a window of my program, another window system of a third party system, such as Notepad.
I found the answer here (sorry the site is in spanish) but it only works with Windows Form.
This is my code.
//get the third party window
public static IntPtr getWindow(string titleName)
{
Process[] pros = Process.GetProcesses(".");
foreach (Process p in pros)
if (p.MainWindowTitle.ToUpper().Contains(titleName.ToUpper()))
return p.MainWindowHandle;
return new IntPtr();
}
//Get my own window
IntPtr ptr = new WindowInteropHelper(this).Handle;
//a window embedded within the other
[DllImport("user32.dll")]
public extern static IntPtr SetParent(IntPtr hWnChild, IntPtr hWndNewParent);
As I said, that works for Windows Forms, but in WPF doesn't work.
Upvotes: 1
Views: 2808
Reputation: 247
Here is the code, the problem was that you need to set position, width, height and repaint the child window in the new parent window.
public void CapturarApp()
{
hWndApp = ScreenFuntion.getWindow("Notepad");
if (hWndApp.ToInt32() > 0)
{
ProgramsEncrustForm.MoveWindow(hWndApp,
0, 0,
Int32.Parse(Width.ToString()),
Int32.Parse(Height.ToString()), 1);
ProgramsEncrustForm.SetParent(hWndApp, new WindowInteropHelper(this).Handle);
}
else
{
hWndApp = IntPtr.Zero;
}
this.Show();
}
And here is the method to move and repaint the window
[System.Runtime.InteropServices.DllImport("user32.dll")]
public extern static int MoveWindow(IntPtr hWnd, int x, int y,
int nWidth, int nHeight, int bRepaint);
Upvotes: 2