WPF 嵌入无边框外部exe程序

 

嵌入步骤

1.Process 运行exe程序

2.获取exe程序的主窗口句柄

3.设置当前应用程序与exe程序的关联

4.将exe程序主窗体移入应用程序

        public const long WS_CHILD = 0x40000000L, WS_POPUP = 0x80000000L;
       public const int GWL_STYLE = (-16);
       public const long WS_CATION = 0x00C00000L;
       public const long WS_CATION_2 = 0x00C0000L;
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CallingConvention = CallingConvention.Cdecl)]
        public static extern long SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindow")]
        public static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private void LoadExe(){

 string exePath = “”//exe存放地址;
            if (!System.IO.File.Exists(exePath))
            {
                return;
            }
            string AppName = exePath.Substring(exePath.LastIndexOf("\\")).Replace("\\", "").Replace(".exe", "");
            if (System.Diagnostics.Process.GetProcessesByName(AppName).Count() > 0)
            {
                System.Diagnostics.Process.GetProcessesByName(AppName)[0].Kill();//结束正在运行的exe
            }
            System.Diagnostics.Process process = new System.Diagnostics.Process(); 从新开启exe
       string arguments = ""//设置程序启动参数;
            process.StartInfo = new System.Diagnostics.ProcessStartInfo(exePath, arguments);
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
     while (process.WaitForInputIdle())
            {

                long oldStyle = NativeMethods.GetWindowLong(process.MainWindowHandle, NativeMethods.GWL_STYLE);
                SetWindowLong(process.MainWindowHandle, GWL_STYLE, oldStyle & (~(WS_CATION | WS_CATION_2)));//修改启动程序的窗口样式为无边框
                SetParent(process.MainWindowHandle, panel.Handle);//设置与主程序关联
                MoveWindow(process.MainWindowHandle, 0, 0, (int)this.AnesGrid.ActualWidth, (int)this.AnesGrid.ActualHeight, true);//嵌入到主程序,并设置窗体位置和大小
                return;
            }

}
        public class NativeMethods
        {
            ///  

            /// 带有外边框和标题的windows的样式 

            ///  

            public const long WS_CAPTION = 0x00C00000L;

            public const long WS_CAPTION_2 = 0X00C0000L;

            // public const long WS_BORDER = 0X0080000L; 
            ///  

            /// window 扩展样式 分层显示 

            ///  

            public const long WS_EX_LAYERED = 0x00080000L;

            public const long WS_CHILD = 0x40000000L;
            ///  

            /// 带有alpha的样式 

            ///  

            public const long LWA_ALPHA = 0x00000002L;
            ///  

            /// 颜色设置 

            ///  

            public const long LWA_COLORKEY = 0x00000001L;
            ///  

            /// window的基本样式 

            ///  

            public const int GWL_STYLE = -16;
            ///  

            /// window的扩展样式 

            ///  

            public const int GWL_EXSTYLE = -20;
            ///  

            /// 设置窗体的样式 

            ///  

            /// 操作窗体的句柄 

            /// 进行设置窗体的样式类型. 

            /// 新样式 

            [System.Runtime.InteropServices.DllImport("User32.dll")]

            public static extern void SetWindowLong(IntPtr handle, int oldStyle, long newStyle);
            ///  

            /// 获取窗体指定的样式. 

            ///  

            /// 操作窗体的句柄 

            /// 要进行返回的样式 

            /// 当前window的样式 

            [System.Runtime.InteropServices.DllImport("User32.dll")]

            public static extern long GetWindowLong(IntPtr handle, int style);
            ///  

            /// 设置窗体的工作区域. 

            ///  

            /// 操作窗体的句柄. 

            /// 操作窗体区域的句柄. 

            /// if set to true [regraw]. 

            /// 返回值 

            [System.Runtime.InteropServices.DllImport("User32.dll")]

            public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);
            ///  

            /// 创建带有圆角的区域. 

            ///  

            /// 左上角坐标的X值. 

            /// 左上角坐标的Y值. 

            /// 右下角坐标的X值. 

            /// 右下角坐标的Y值. 

            /// 圆角椭圆的width. 

            /// 圆角椭圆的height. 

            /// hRgn的句柄 

            [System.Runtime.InteropServices.DllImport("gdi32.dll")]

            public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height);

            ///  

            /// Sets the layered window attributes. 

            ///  

            /// 要进行操作的窗口句柄 

            /// RGB的值 

            /// Alpha的值,透明度 

            /// 附带参数 

            /// true or false 

            [System.Runtime.InteropServices.DllImport("User32.dll")]
            public static extern bool SetLayeredWindowAttributes(IntPtr handle, ulong colorKey, byte alpha, long flags);
        }

 

 

句柄:程序为每个对象分配一个地址,句柄是对应这个分配地址的链接,即句柄表示这个对象,每个对象有且只有一个句柄。操作句柄实则就是操作该对象

win32API参考手册:http://www.yfvb.com/help/win32sdk/index.htm?page=html/4luzha.htm

 

你可能感兴趣的:(WPF 嵌入无边框外部exe程序)