博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[分享]外部exe窗体嵌入winform 分类: .NET ...
阅读量:5227 次
发布时间:2019-06-14

本文共 8324 字,大约阅读时间需要 27 分钟。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.Runtime.InteropServices;using System.IO;using System.Drawing.Design; namespace HCDL{    public class ShowForm    {        //-------------------        //Action
appIdleAction = null; EventHandler appIdleEvent = null; Control ParentCon = null; string strGUID = ""; public ShowForm(Control C,string Titlestr) { appIdleEvent = new EventHandler(Application_Idle); ParentCon = C; strGUID = Titlestr; } ///
/// 将属性AppFilename指向的应用程序打开并嵌入此容器 /// public IntPtr Start(string FileNameStr) { if (m_AppProcess != null) { Stop(); } try { ProcessStartInfo info = new ProcessStartInfo(FileNameStr); info.UseShellExecute = true; info.WindowStyle = ProcessWindowStyle.Minimized; m_AppProcess = System.Diagnostics.Process.Start(info); m_AppProcess.WaitForInputIdle(); Application.Idle += appIdleEvent; } catch { if (m_AppProcess != null) { if (!m_AppProcess.HasExited) m_AppProcess.Kill(); m_AppProcess = null; } } return m_AppProcess.Handle; } ///
/// 确保应用程序嵌入此容器 /// ///
///
void Application_Idle(object sender, EventArgs e) { if (this.m_AppProcess == null || this.m_AppProcess.HasExited) { this.m_AppProcess = null; Application.Idle -= appIdleEvent; return; } if (m_AppProcess.MainWindowHandle == IntPtr.Zero) return; Application.Idle -= appIdleEvent; EmbedProcess(m_AppProcess, ParentCon); } ///
/// 应用程序结束运行时要清除这里的标识 /// ///
///
void m_AppProcess_Exited(object sender, EventArgs e) { m_AppProcess = null; } ///
/// 将属性AppFilename指向的应用程序关闭 /// public void Stop() { if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero) { try { if (!m_AppProcess.HasExited) m_AppProcess.Kill(); } catch (Exception) { } m_AppProcess = null; } } #region 属性 ///
/// application process /// Process m_AppProcess = null; ///
/// 标识内嵌程序是否已经启动 /// public bool IsStarted { get { return (this.m_AppProcess != null); } } #endregion 属性 #region Win32 API [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)] private static extern long GetWindowLong(IntPtr hwnd, int nIndex); public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong) { if (IntPtr.Size == 4) { return SetWindowLongPtr32(hWnd, nIndex, dwNewLong); } return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); } [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", SetLastError = true)] private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags); [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr GetParent(IntPtr hwnd); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int SWP_NOOWNERZORDER = 0x200; private const int SWP_NOREDRAW = 0x8; private const int SWP_NOZORDER = 0x4; private const int SWP_SHOWWINDOW = 0x0040; private const int WS_EX_MDICHILD = 0x40; private const int SWP_FRAMECHANGED = 0x20; private const int SWP_NOACTIVATE = 0x10; private const int SWP_ASYNCWINDOWPOS = 0x4000; private const int SWP_NOMOVE = 0x2; private const int SWP_NOSIZE = 0x1; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 0x10000000; private const int WM_CLOSE = 0x10; private const int WS_CHILD = 0x40000000; private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标} private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活} private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL} private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活} private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活} private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED} private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活} private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL} private const int SW_MINIMIZE = 6; //{最小化, 不激活} private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE} private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE} private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL} private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL} private const int SW_MAX = 10; //{同 SW_SHOWNORMAL} #endregion Win32 API ///
/// 将指定的程序嵌入指定的控件 /// private void EmbedProcess(Process app, Control control) { // Get the main handle if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return; try { // Put it into this form SetParent(app.MainWindowHandle, control.Handle); } catch (Exception) { } try { // Remove border and whatnot SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE); SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID); } catch (Exception) { } try { // Move the window to overlay it on this window MoveWindow(app.MainWindowHandle, -20, -20, control.Width, control.Height-10, true); } catch (Exception) { } } [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); const int WM_SETTEXT = 0x000C; }}

PS:以前是3.5版本。现在改成2.0也能用的了

用法

ShowForm Sf = new ShowForm(this, "仅供内部使用" + System.Guid.NewGuid().ToString());            ProxHandle= Sf.Start(Application.StartupPath + "\\ProxyThorn.exe");

原帖地址:

转载于:https://www.cnblogs.com/configman/p/4657539.html

你可能感兴趣的文章
算术表达式解析(第一版)
查看>>
java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
查看>>
兼容各种浏览器的透明层效果
查看>>
软件工程概论课总结
查看>>
UVA11255 Necklace Burnside、组合
查看>>
Docker生产实践(六)
查看>>
机器学习实战5-AdaBoost
查看>>
web-11. 层叠式表的属性与滤镜
查看>>
Vue
查看>>
表变量与临时表的优缺点(转)
查看>>
shell脚本图书
查看>>
UNIX环境高级编程——线程限制
查看>>
UNIX网络编程——原始套接字SOCK_RAW
查看>>
TCP发送源码学习(1)--tcp_sendmsg
查看>>
使用两个不同类型的数据进行加法计算时,使用异常处理语句捕获由于数据类型错误而出现的异常,发生生成错误。是否继续并运行上次的成功生成?...
查看>>
python-三级菜单和购物车程序
查看>>
web开发灵感推荐--34个有吸引力的电影网站设计灵感
查看>>
sql操作
查看>>
条件断点 符号断点
查看>>
第二十三模板 18.3.5 位集合
查看>>