exe程序嵌入Winform窗体(转载)

1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform;

2.新建一个类文件,方便引用,命名为:exetowinform;

3.Mainform中cs代码如下:

 

[csharp]  view plain  copy
 
 print?
  1. exetowinform fr = null;  
  2.       private void button1_Click(object sender, EventArgs e)  
  3.       {              
  4.           OpenFileDialog Oppf = new OpenFileDialog();  
  5.           Oppf.ShowDialog();  
  6.           if (Oppf.FileName != "")  
  7.           {  
  8.               panel1.Controls.Clear();  
  9.               fr = new exetowinform(panel1, "");  
  10.               fr.Start(Oppf.FileName);  
  11.           }  
  12.       }  

4.exetowinform类文件代码如下:

 

 

[csharp]  view plain  copy
 
 print?
  1. public class exetowinform  
  2.    {  
  3.        EventHandler appIdleEvent = null;  
  4.        Control ParentCon = null;  
  5.        string strGUID = "";  
  6.   
  7.        public exetowinform(Control C, string Titlestr)  
  8.        {  
  9.            appIdleEvent = new EventHandler(Application_Idle);  
  10.            ParentCon = C;  
  11.            strGUID = Titlestr;  
  12.        }  
  13.   
  14.        /// <summary>  
  15.        /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器  
  16.        /// </summary>  
  17.        public IntPtr Start(string FileNameStr)  
  18.        {  
  19.            if (m_AppProcess != null)  
  20.            {  
  21.                Stop();  
  22.            }  
  23.            try  
  24.            {  
  25.                ProcessStartInfo info = new ProcessStartInfo(FileNameStr);  
  26.                info.UseShellExecute = true;  
  27.                info.WindowStyle = ProcessWindowStyle.Minimized;  
  28.                m_AppProcess = System.Diagnostics.Process.Start(info);  
  29.                m_AppProcess.WaitForInputIdle();  
  30.                Application.Idle += appIdleEvent;  
  31.            }  
  32.            catch  
  33.            {  
  34.                if (m_AppProcess != null)  
  35.                {  
  36.                    if (!m_AppProcess.HasExited)  
  37.                        m_AppProcess.Kill();  
  38.                    m_AppProcess = null;  
  39.                }  
  40.            }  
  41.            return m_AppProcess.Handle;  
  42.        }  
  43.        /// <summary>  
  44.        /// 确保应用程序嵌入此容器  
  45.        /// </summary>  
  46.        /// <param name="sender"></param>  
  47.        /// <param name="e"></param>  
  48.        void Application_Idle(object sender, EventArgs e)  
  49.        {  
  50.            if (this.m_AppProcess == null || this.m_AppProcess.HasExited)  
  51.            {  
  52.                this.m_AppProcess = null;  
  53.                Application.Idle -= appIdleEvent;  
  54.                return;  
  55.            }  
  56.   
  57.            while (m_AppProcess.MainWindowHandle == IntPtr.Zero)  
  58.            {  
  59.                Thread.Sleep(100);  
  60.                m_AppProcess.Refresh();  
  61.            }  
  62.            Application.Idle -= appIdleEvent;  
  63.            EmbedProcess(m_AppProcess, ParentCon);  
  64.        }  
  65.        /// <summary>  
  66.        /// 应用程序结束运行时要清除这里的标识  
  67.        /// </summary>  
  68.        /// <param name="sender"></param>  
  69.        /// <param name="e"></param>  
  70.        void m_AppProcess_Exited(object sender, EventArgs e)  
  71.        {  
  72.            m_AppProcess = null;  
  73.        }  
  74.        /// <summary>  
  75.        /// 将属性<code>AppFilename</code>指向的应用程序关闭  
  76.        /// </summary>  
  77.        public void Stop()  
  78.        {  
  79.            if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero)  
  80.            {  
  81.                try  
  82.                {  
  83.                    if (!m_AppProcess.HasExited)  
  84.                        m_AppProcess.Kill();  
  85.                }  
  86.                catch (Exception)  
  87.                {  
  88.                }  
  89.                m_AppProcess = null;  
  90.            }  
  91.        }  
  92.  
  93.  
  94.        #region 属性  
  95.        /// <summary>  
  96.        /// application process  
  97.        /// </summary>  
  98.        Process m_AppProcess = null;  
  99.   
  100.        /// <summary>  
  101.        /// 标识内嵌程序是否已经启动  
  102.        /// </summary>  
  103.        public bool IsStarted { get { return (this.m_AppProcess != null); } }  
  104.  
  105.        #endregion 属性  
  106.  
  107.        #region Win32 API  
  108.        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,  
  109.             CharSet = CharSet.Unicode, ExactSpelling = true,  
  110.             CallingConvention = CallingConvention.StdCall)]  
  111.        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);  
  112.   
  113.        [DllImport("user32.dll", SetLastError = true)]  
  114.        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
  115.   
  116.        [DllImport("user32.dll", SetLastError = true)]  
  117.        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);  
  118.   
  119.        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]  
  120.        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);  
  121.   
  122.        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)  
  123.        {  
  124.            if (IntPtr.Size == 4)  
  125.            {  
  126.                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);  
  127.            }  
  128.            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);  
  129.        }  
  130.        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]  
  131.        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);  
  132.        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]  
  133.        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);  
  134.   
  135.        [DllImport("user32.dll", SetLastError = true)]  
  136.        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);  
  137.   
  138.        [DllImport("user32.dll", SetLastError = true)]  
  139.        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);  
  140.   
  141.        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]  
  142.        private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);  
  143.   
  144.        [DllImport("user32.dll", SetLastError = true)]  
  145.        private static extern IntPtr GetParent(IntPtr hwnd);  
  146.   
  147.        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]  
  148.        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  
  149.   
  150.        private const int SWP_NOOWNERZORDER = 0x200;  
  151.        private const int SWP_NOREDRAW = 0x8;  
  152.        private const int SWP_NOZORDER = 0x4;  
  153.        private const int SWP_SHOWWINDOW = 0x0040;  
  154.        private const int WS_EX_MDICHILD = 0x40;  
  155.        private const int SWP_FRAMECHANGED = 0x20;  
  156.        private const int SWP_NOACTIVATE = 0x10;  
  157.        private const int SWP_ASYNCWINDOWPOS = 0x4000;  
  158.        private const int SWP_NOMOVE = 0x2;  
  159.        private const int SWP_NOSIZE = 0x1;  
  160.        private const int GWL_STYLE = (-16);  
  161.        private const int WS_VISIBLE = 0x10000000;  
  162.        private const int WM_CLOSE = 0x10;  
  163.        private const int WS_CHILD = 0x40000000;  
  164.   
  165.        private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标}  
  166.        private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活}  
  167.        private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}  
  168.        private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}  
  169.        private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}  
  170.        private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}  
  171.        private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活}  
  172.        private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}  
  173.        private const int SW_MINIMIZE = 6; //{最小化, 不激活}  
  174.        private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}  
  175.        private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}  
  176.        private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}  
  177.        private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}  
  178.        private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}  
  179.  
  180.        #endregion Win32 API  
  181.   
  182.        /// <summary>  
  183.        /// 将指定的程序嵌入指定的控件  
  184.        /// </summary>  
  185.        private void EmbedProcess(Process app, Control control)  
  186.        {  
  187.            // Get the main handle  
  188.            if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;  
  189.            try  
  190.            {  
  191.                // Put it into this form  
  192.                SetParent(app.MainWindowHandle, control.Handle);  
  193.            }  
  194.            catch (Exception)  
  195.            { }  
  196.            try  
  197.            {  
  198.                // Remove border and whatnot                 
  199.                SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);  
  200.                SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);  
  201.            }  
  202.            catch (Exception)  
  203.            { }  
  204.            try  
  205.            {  
  206.                // Move the window to overlay it on this window  
  207.                MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);  
  208.            }  
  209.            catch (Exception)  
  210.            { }  
  211.        }  
  212.   
  213.        [DllImport("User32.dll", EntryPoint = "SendMessage")]  
  214.        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);  
  215.   
  216.        const int WM_SETTEXT = 0x000C;  
  217.    }  


5.最后结果如图:

 

转自:http://blog.csdn.net/lisenyang/article/details/18303971

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值