C# 嵌入第三方EXE界面到panel中

C#可以通过windows API,将第三方程序嵌入到panel中,并且可以隐藏程序边框。
问题:
焦点在内部程序时,主窗口失去焦点;
与内部EXE如何通讯?

代码如下:

public partial class FrmIn : Form
{
    public FrmIn()
    {
        InitializeComponent();
    }

    [DllImport("User32.dll", EntryPoint = "SetParent")]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll", EntryPoint = "ShowWindow")]
    private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

    [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")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    const int WS_THICKFRAME = 262144;
    const int WS_BORDER = 8388608;
    const int GWL_STYLE = -16;

    private Process proApp = null;

    private void FrmIn_Load(object sender, EventArgs e)
    {
        string fexePath = @"notepad.exe"; // 外部exe位置

        proApp = new Process();
        proApp.StartInfo.FileName = fexePath;
        proApp.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
        proApp.Start();
        proApp.WaitForInputIdle();

        Thread.Sleep(1000);

        IntPtr wnd = proApp.MainWindowHandle;

        Int32 wndStyle = GetWindowLong(wnd, GWL_STYLE);
        wndStyle &= ~WS_BORDER;
        wndStyle &= ~WS_THICKFRAME;
        SetWindowLong(wnd, GWL_STYLE, wndStyle);

        SetParent(proApp.MainWindowHandle, panel1.Handle);
        ShowWindow(proApp.MainWindowHandle, (int)ProcessWindowStyle.Maximized);
    }

    private void panel1_SizeChanged(object sender, EventArgs e)
    {
        if (this.proApp == null)
        {
            return;
        }

        if (this.proApp.MainWindowHandle != IntPtr.Zero)
        {
            MoveWindow(this.proApp.MainWindowHandle, 0, 0, panel1.Width, panel1.Height, true);
        }
    }
}

 

C# 嵌入第三方EXE界面到panel中

上一篇:Delphi中AssignFile函数


下一篇:NX二次开发-UFUN将建模绝对空间中的点映射到工程图坐标UF_VIEW_map_model_to_drawing