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
        {
            /// <summary> 

            /// 帶有外邊框和標題的windows的樣式 

            /// </summary> 

            public const long WS_CAPTION = 0x00C00000L;

            public const long WS_CAPTION_2 = 0X00C0000L;

            // public const long WS_BORDER = 0X0080000L; 
            /// <summary> 

            /// window 擴展樣式 分層顯示 

            /// </summary> 

            public const long WS_EX_LAYERED = 0x00080000L;

            public const long WS_CHILD = 0x40000000L;
            /// <summary> 

            /// 帶有alpha的樣式 

            /// </summary> 

            public const long LWA_ALPHA = 0x00000002L;
            /// <summary> 

            /// 顏色設置 

            /// </summary> 

            public const long LWA_COLORKEY = 0x00000001L;
            /// <summary> 

            /// window的基本樣式 

            /// </summary> 

            public const int GWL_STYLE = -16;
            /// <summary> 

            /// window的擴展樣式 

            /// </summary> 

            public const int GWL_EXSTYLE = -20;
            /// <summary> 

            /// 設置窗體的樣式 

            /// </summary> 

            /// <param name="handle">操作窗體的句柄</param> 

            /// <param name="oldStyle">進行設置窗體的樣式類型.</param> 

            /// <param name="newStyle">新樣式</param> 

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

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

            /// 獲取窗體指定的樣式. 

            /// </summary> 

            /// <param name="handle">操作窗體的句柄</param> 

            /// <param name="style">要進行返回的樣式</param> 

            /// <returns>當前window的樣式</returns> 

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

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

            /// 設置窗體的工作區域. 

            /// </summary> 

            /// <param name="handle">操作窗體的句柄.</param> 

            /// <param name="handleRegion">操作窗體區域的句柄.</param> 

            /// <param name="regraw">if set to <c>true</c> [regraw].</param> 

            /// <returns>返回值</returns> 

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

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

            /// 創建帶有圓角的區域. 

            /// </summary> 

            /// <param name="x1">左上角坐標的X值.</param> 

            /// <param name="y1">左上角坐標的Y值.</param> 

            /// <param name="x2">右下角坐標的X值.</param> 

            /// <param name="y2">右下角坐標的Y值.</param> 

            /// <param name="width">圓角橢圓的width.</param> 

            /// <param name="height">圓角橢圓的height.</param> 

            /// <returns>hRgn的句柄</returns> 

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

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

            /// <summary> 

            /// Sets the layered window attributes. 

            /// </summary> 

            /// <param name="handle">要進行操作的窗口句柄</param> 

            /// <param name="colorKey">RGB的值</param> 

            /// <param name="alpha">Alpha的值,透明度</param> 

            /// <param name="flags">附帶參數</param> 

            /// <returns>true or false</returns> 

            [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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM