반응형

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window1" Height="514" Width="587.496">
	<Grid>
		<WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="438" Margin="10,35,0,0" VerticalAlignment="Top" Width="559"/>
		<Button x:Name="button1" Content="Hosting" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>

	</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
	public partial class Window1 : Window
	{
		private System.Windows.Forms.Panel _panel;
		private Process _process;

		public Window1()
		{
			InitializeComponent();
			_panel = new System.Windows.Forms.Panel();
			windowsFormsHost1.Child = _panel;

			button1.Click += button1_Click;
		}

		[DllImport("user32.dll")]
		private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

		[DllImport("user32.dll", SetLastError = true)]
		private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

		[DllImport("user32")]
		private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

		[DllImport("user32")]
		private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

		private const int SWP_NOZORDER = 0x0004;
		private const int SWP_NOACTIVATE = 0x0010;
		private const int GWL_STYLE = -16;
		private const int WS_CAPTION = 0x00C00000;
		private const int WS_THICKFRAME = 0x00040000;

		private void button1_Click(object sender, RoutedEventArgs e)
		{
			ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
			_process = Process.Start(psi);
			_process.WaitForInputIdle();
			SetParent(_process.MainWindowHandle, _panel.Handle);

			// remove control box
			int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
			style = style & ~WS_CAPTION & ~WS_THICKFRAME;
			SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);

			// resize embedded application & refresh
			ResizeEmbeddedApp();
		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			base.OnClosing(e);
			if (_process != null)
			{
				_process.Refresh();
				_process.Close();
			}
		}

		private void ResizeEmbeddedApp()
		{
			if (_process == null)
				return;

			SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)_panel.ClientSize.Width, (int)_panel.ClientSize.Height, SWP_NOZORDER | SWP_NOACTIVATE);
		}

		protected override Size MeasureOverride(Size availableSize)
		{
			Size size = base.MeasureOverride(availableSize);
			ResizeEmbeddedApp();
			return size;
		}
	}
}

반응형

+ Recent posts