【C# 界面嵌入Exe】

C# 界面嵌入其他应用程序

在界面中嵌入其他应用程序需要执行的事情是
1) 运行应用程序
2)嵌入应用程序
运行环境VS2019

        [DllImport("User32.dll ", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);

运行应用程序

运行应用程序,有些需要完整的路径,有些只需要应用程序名称就可以了,例如cmd.exe
我们运用进程去控制cmd.exe 运行和关闭。

嵌入应用程序

我们在FORM中插入Panel,然后再Panel嵌入正在执行的.exe文件。
#########################
代码如下:

            cmdP = new Process();
            cmdP.StartInfo.FileName = "cmd.exe";
            cmdP.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
            cmdP.Start();
            Thread.Sleep(200);
            SetWindowPos(cmdP.MainWindowHandle, IntPtr.Zero, this.panel1.Location.X, this.panel1.Location.Y, panel1.Width,panel1.Height, 0);
            SetParent(cmdP.MainWindowHandle, panel1.Handle); 
            ShowWindow(cmdP.MainWindowHandle, 3);
           

运行结果:
在这里插入图片描述
在此例子的基础上扩展应用需要考虑
1)目录路径
2)外部EXE界面尺寸
3)界面的开关:有的界面很简单就可以一次过关闭,有的关闭的时候,关得不好会会有其他问题。
4)调用多个外部界面的时候,最好使用线程或记得进行异常捕获处理,让多个外部界面顺利显示。

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值