C# 以嵌入到窗体的方式打开外部exe

---------------------------------------------------------------------

代码已移至:

http://www.cnblogs.com/kongfl888/p/3351733.html

--------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Text;
        
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
        
namespace War3Screen
{
    /// 
    /// 以嵌入到窗体的方式打开外部exe--kongfl888 2013
    /// 
    class OpenExeClass
    {
        static Process process = null;
        static IntPtr appWin;
        private static string exeName = "";
        
        
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool
        
repaint);
        
        //[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
        //     CharSet = CharSet.Unicode, ExactSpelling = true,
        //     CallingConvention = CallingConvention.StdCall)]
        //private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
        
        //[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        //private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
        
        //[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
        //private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
                
        
        //[DllImport("user32.dll", SetLastError = true)]
        //private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long 
        
y, long cx, long cy, long wFlags);
        
        
        //[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        //private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);
        
        //[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        //private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
        
        //[DllImport("user32.dll")]
        //[return: MarshalAs(UnmanagedType.Bool)]
        //static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
        
        //[StructLayout(LayoutKind.Sequential)]
        //public struct RECT
        //{
        //    public int Left;                             //最左坐标
        //    public int Top;                             //最上坐标
        //    public int Right;                           //最右坐标
        //    public int Bottom;                        //最下坐标
        //}
        
        //private const int SWP_NOOWNERZORDER = 0x200;
        //private const int SWP_NOREDRAW = 0x8;
        //private const int SWP_NOZORDER = 0x4;
        //private const int SWP_SHOWWINDOW = 0x0040;
        //private const int WS_EX_MDICHILD = 0x40;
        //private const int SWP_FRAMECHANGED = 0x20;
        //private const int SWP_NOACTIVATE = 0x10;
        //private const int SWP_ASYNCWINDOWPOS = 0x4000;
        //private const int SWP_NOMOVE = 0x2;
        //private const int SWP_NOSIZE = 0x1;
        //private const int GWL_STYLE = (-16);
        //private const int WS_VISIBLE = 0x10000000;
        //private const int WM_CLOSE = 0x10;
        //private const int WS_CHILD = 0x40000000;
        //private const int SW_HIDE = 0;
        
        
}


主窗体退出时写FormClosed事件函数,调用CloseExe()函数



在控件改变大小的时候,重新MoveWindow()即可,如:

        private void splitContainer1_Panel2_Resize(object sender, EventArgs e)

        {

            if (this.appWin != IntPtr.Zero)

            {

                MoveWindow(appWin, 0, 0, this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height, true);

            }

            base.OnResize(e);

        }


你可能感兴趣的:(C#,度娘搬家)