您的位置:首页 > 其它

Winform嵌入其它应用程序

2017-10-23 12:09 357 查看
Options:

private void ShowBrowser(string url)
{
if (dkPnl.Children.Count > 0)
{
WindowsFormsHost whst = dkPnl.Children[0] as WindowsFormsHost;
whst.Dispose();
foreach (Process p in Process.GetProcessesByName("MyBrowser"))
{
p.Kill();
}
}

var host = new ApplicationHost()
{
File = @"MyBrowser.exe",
Arguments = string.Empty,
HideApplicationTitleBar = true,
Dock = System.Windows.Forms.DockStyle.Fill,
BorderStyle = System.Windows.Forms.BorderStyle.None
};
host.ProcessLoaded += host_ProcessLoaded;
host.ProcessUnLoaded += host_ProcessUnLoaded;

WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
windowsFormsHost.Child = host;
dkPnl.Children.Add(windowsFormsHost);
}

private Dictionary<IntPtr, ApplicationHost> _hostPool;
private Dictionary<IntPtr, ApplicationHost> m_HostPool
{
get
{
return _hostPool ?? (_hostPool = new Dictionary<IntPtr, ApplicationHost>());
}
}

void host_ProcessLoaded(object sender, EventArgs e)
{
var host = sender as ApplicationHost;
m_HostPool.Add(host.MainWindowHandle, host);
}

void host_ProcessUnLoaded(object sender, EventArgs e)
{
var host = sender as ApplicationHost;

var parent = host.Parent;
if (parent != null && !parent.IsDisposed)
{
parent.Dispose();
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: