C# 使用Win32 API将1个EXE程序嵌入另1个程序中

已经干到天快亮了,就不废话直接贴点儿代码吧

        private const int WS_THICKFRAME = 262144;
        private const int WS_BORDER = 8388608;

        /// <summary>
        /// 查找窗口
        ///第一个参数是窗口的标题,第二个参数可直接用 null
        ///通过窗口的标题查找对应的窗口
        /// </summary>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //设置窗口的父窗体
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //该api用于嵌入到窗口中运行

        //获取窗口样式
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        //设置窗口样式
        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        //设置窗口位置
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

     //设置过后就不再重复设置
     bool isSeted = false;

        //传入子窗体的句柄(可以使用FindWindow获取)
        public void SetWindowParent(IntPtr wnd)
        {
            if (!isSeted)
            {
                //子窗口句柄,父窗口句柄(我这里用的Winform里的panel控件的句柄,这样就会将我的子窗口嵌入到panel里面)
                SetParent(wnd, unityPanel.Handle);

                Int32 wndStyle = GetWindowLong(wnd, -16);
                wndStyle &= ~WS_BORDER;
                wndStyle &= ~WS_THICKFRAME;
                SetWindowLong(wnd, -16, wndStyle);
                MoveWindow(wnd, 0, 0, unityPanel.Width, unityPanel.Height, true);

                isSeted = true;
            }
        }    

 

posted @ 2017-09-26 04:11  叶图大师  阅读(1546)  评论(0编辑  收藏  举报