微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Winform嵌入其它应用程序

Options:

using CommandLine;
 System;
 System.Collections.Generic;
 System.Linq;
 System.Text;
 System.Threading.Tasks;

namespace SunCreate.CombatPlatform.Client
{
    public class Options
    {
        [Option("h",handlerequired = true)]
        int Handle { get; set; }

        [Option(bbrowser"public Boolean Isbrowser { ; }
    }

}
View Code

ApplicationHost:

 System.ComponentModel;
 System.Diagnostics;
 System.Runtime.InteropServices;
 System.Threading;
 System.Windows.Forms;

 SunCreate.CombatPlatform.Client
{
    /// <summary>
    /// 
    </summary>
     ApplicationHost : UserControl
    {
        #region PInvoke
        [DllImport(user32.dllprivate static extern long SetParent(IntPtr hWndChild,IntPtr hWndNewParent);

        [DllImport(bool MoveWindow(IntPtr hwnd,int x,1)">int y,1)">int cx,1)">int cy,1)">bool repaint);

        [DllImport(SetwindowLongAlong SetwindowLong(IntPtr hwnd,1)">int nIndex,1)">int dwNewLong);
        #endregion


        #region Const
        const int GWL_STYLE = -16;
        int WS_VISIBLE = 0x10000000#region Var
        private Boolean _autoLoadProcess = private Process _process;
        string _file;
         _arguments;
        #region Private Property
        <summary>
        /// Gets or sets the m_ process.
        </summary>
        <value>
         The m_ process.
        </value>
         Process m_Process
        {
            get
            {
                return _process;
            }
            if (_process == value)
                    ;

                if (value == null)
                    UnloadProcess();

                _process = value;
            }
        }
        #region Public Property
         Gets or sets the auto load process.
         The auto load process.
        public Boolean AutoLoadProcess
        {
             _autoLoadProcess;
            }
            
            {
                _autoLoadProcess = value;
            }
        }

         Gets or sets the hide application title bar.
         The hide application title bar.
        public Boolean HideApplicationTitleBar { ; }

         Gets or sets the file.
         The file.
         File
        {
            return _file ?? .Empty;
            }
            
            {
                _file = Gets or sets the arguments.
         The arguments.
         Arguments
        {
            return _arguments ?? 
            {
                _arguments = Gets the main window handle.
         The main window handle.
         IntPtr MainWindowHandle 
        {
            return m_Process == null ? IntPtr.Zero : m_Process.MainWindowHandle;
            }
        }

         Gets the main window title.
         The main window title.
         MainWindowTitle
        {
            null ? .Empty : m_Process.MainWindowTitle;
            }
        }
        #region Constructor & DeConstructor
         Initializes a new instance of the <see cref="ApplicationHost" /> class.
        </summary>
         ApplicationHost()
        {
            this.Load += ApplicationHost_Load;
            this.ProcessLoaded += ApplicationHost_ProcessLoaded;
            this.ProcessUnLoaded += ApplicationHost_ProcessUnLoaded;
        }

         Finalizes an instance of the </summary>
        ~ApplicationHost()
        {
            m_Process = ;
        }
        #region Event
        event EventHandler ProcessLoaded;
         EventHandler ProcessUnLoaded;
        #region Protected Method
        <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void dispose( disposing)
        {
            if (disposing)
            {
                m_Process = ;
            }
            base.dispose(disposing);
        }

         Raises the <see cref="E:ProcessLoaded" /> event.
        <param name="e">The <see cref="EventArgs" /> instance containing the event data.void OnProcessLoaded(EventArgs e)
        {
            if (ProcessLoaded == )
                ;
            ProcessLoaded(this,e);
        }

        <see cref="E:ProcessUnLoaded" /> OnProcessUnLoaded(EventArgs e)
        {
            if (ProcessUnLoaded == ;
            ProcessUnLoaded(#region Public Method
         Loads the process.
         LoadProcess()
        {
            if (m_Process != )
            {
                var startInfo = m_Process.StartInfo;
                if (startInfo.FileName != this.File || startInfo.Arguments != .Arguments)
                    m_Process = ;
                else
                    ;
            }

            m_Process = new Process()
            {
                SynchronizingObject =  processstartinfo()
                {
                    FileName = File,Arguments = .Arguments
                }
            };
        
            m_Process.Start();

            m_Process.WaitForInputIdle();
            while (!m_Process.HasExited && m_Process.MainWindowHandle == IntPtr.Zero)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }

            m_Process.EnableRaisingEvents = ;

            m_Process.Exited += m_Process_Exited;

            var handle = m_Process.MainWindowHandle;

             (HideApplicationTitleBar)
                SetwindowLong(handle,GWL_STYLE,WS_VISIBLE);

            SetParent(handle,.Handle);

            MoveWindow(handle,0,1)">this.Width,1)">this.Height,1)">);

            OnProcessLoaded(EventArgs.Empty);
        }

         Unloads the process.
         UnloadProcess()
        {
            if (m_Process == ;

             (m_Process.HasExited)
                ;

            m_Process.CloseMainWindow();
            m_Process.WaitForExit();

            null && !m_Process.HasExited)
                m_Process.Kill();

            OnProcessUnLoaded(EventArgs.Empty);
        }

         Reloads the process.
         ReloadProcess()
        {
            UnloadProcess();
            LoadProcess();
        }
        #region Event Process
         Handles the Load event of the ApplicationHost control.
        <param name="sender">The source of the event.</param>
        void ApplicationHost_Load(object sender,EventArgs e)
        {
            if (Process.GetCurrentProcess().ProcessName.Equals(devenvnoreCase))
                 (AutoLoadProcess)
                LoadProcess();
        }

         Handles the Resize event of the ApplicationHost control.
        void ApplicationHost_Resize(if (handle != IntPtr.Zero)
                MoveWindow(handle,1)">);
        }

         Handles the ProcessLoaded event of the ApplicationHost control.
        void ApplicationHost_ProcessLoaded(this.Resize += ApplicationHost_Resize;
        }

         Handles the ProcessUnLoaded event of the ApplicationHost control.
        void ApplicationHost_ProcessUnLoaded(this.Resize -= Handles the Exited event of the m_Process control.
        void m_Process_Exited(;

            OnProcessUnLoaded(EventArgs.Empty);
        }
        #endregion
    }
}
View Code

代码

void Showbrowser( 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 =  ApplicationHost()
    {
        File = @"Mybrowser.exe.Empty,HideApplicationTitleBar =  System.Windows.Forms.DockStyle.Fill,BorderStyle = System.Windows.Forms.BorderStyle.None
    };
    host.ProcessLoaded += host_ProcessLoaded;
    host.ProcessUnLoaded += host_ProcessUnLoaded;

    windowsformshost windowsformshost =  windowsformshost();
    windowsformshost.Child = host;
    dkPnl.Children.Add(windowsformshost);
}

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

void host_ProcessLoaded(var host = sender  ApplicationHost;
    m_HostPool.Add(host.MainWindowHandle,host);
}

void host_ProcessUnLoaded( ApplicationHost;

    var parent = host.Parent;
    if (parent != parent.Isdisposed)
    {
        parent.dispose();
    }
}
View Code

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐