码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-11-07 13:08:15      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:parent   ret   ext   通讯   turn   inf   hwnd   ESS   point   

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中

标签:parent   ret   ext   通讯   turn   inf   hwnd   ESS   point   

原文地址:https://www.cnblogs.com/zjfree/p/11811263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!