C#Winform窗体中嵌入exe文件

1,效果以嵌入Modbus Slave为例:

2,代码:

 public partial class Form1 : Form
    {
        //设置嵌入exe的常量
        private const int nIndex = -16;
        private const int dwNewLong = 0x10000000;
        Process m_AppProcess;
        public Form1()
        {
            InitializeComponent();
            btnApp.Enabled = false;
        }
        private void btnAppFilePath_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "可执行文件*.exe|*.exe";
                ofd.Multiselect = false;
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    lblExeFilePath.Text = ofd.FileName;
                    btnApp.Enabled = true;
                }
            }
        }
        private void btnApp_Click(object sender, EventArgs e)
        {
            if (lblExeFilePath.Text.Trim().Length == 0)
            {
                MessageBox.Show("请先选择需要执行的exe文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int num = panel1.Controls.Count;
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = lblExeFilePath.Text.Trim();
            info.UseShellExecute = false;
            info.CreateNoWindow = false;
            info.WindowStyle = ProcessWindowStyle.Minimized;
            m_AppProcess = Process.Start(info);
            if (m_AppProcess.WaitForInputIdle(-1))
            {
                System.Threading.Thread.Sleep(500);
                if (EmbedProcess( panel1))
                {
                    return;
                }
            }
            MessageBox.Show("启动失败!");
        }
        /// <summary>
        /// 将指定路径的Exe文件嵌入到指定的窗口容器中
        /// </summary>
        /// <param name="processPath">进程路径</param>
        /// <param name="sleepTime">等待启动时间,单位ms</param>
        /// <param name="parentControl">父容器</param>
        /// <returns></returns>
        bool EmbedProcess( Control parentControl)
        {
            //等待应用启动
            //   EmbedProcess(path, 1000, panel1);
            // System.Threading.Thread.Sleep(sleepTime);
            //  Environment.Is64BitOperatingSystem
            //获取该进程的句柄
            try
            {
                SetParent(m_AppProcess.MainWindowHandle, parentControl.Handle);
                //去除边框
                //判断系统位数
                if (IntPtr.Size == 4)
                {
                    //32位系统
                    SetWindowLongPtr32(new HandleRef(this, m_AppProcess.MainWindowHandle), nIndex, dwNewLong);
                }
                else
                {
                    //64位系统
                    SetWindowLongPtr64(new HandleRef(this, m_AppProcess.MainWindowHandle), nIndex, dwNewLong);
                }
                //移动窗体
                MoveWindow(m_AppProcess.MainWindowHandle, 0, 0, parentControl.Width, parentControl.Height, true);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        //将子窗体嵌入到父窗体,可用于在窗体中嵌入Exe文件
        [DllImport("user32.dll", SetLastError = true)]
        static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        //32位系统使用,作用去除边框
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
        //64位系统使用,作用去除边框
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
        //移动窗体
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
    }

相关推荐

  1. C#WPF更改图标和生成exe文件的图标实例

    2024-07-09 22:16:09       32 阅读
  2. qt 嵌入到任务栏

    2024-07-09 22:16:09       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 22:16:09       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 22:16:09       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 22:16:09       43 阅读
  4. Python语言-面向对象

    2024-07-09 22:16:09       54 阅读

热门阅读

  1. 【Rust入门】猜数游戏

    2024-07-09 22:16:09       24 阅读
  2. NVIDIA H100 Tensor Core GPU摘要

    2024-07-09 22:16:09       17 阅读
  3. 释放计算潜能:Mojo模型与分布式训练的融合之道

    2024-07-09 22:16:09       18 阅读
  4. 环境构建大师:精通Conda中的conda create命令

    2024-07-09 22:16:09       20 阅读
  5. 我的创作4096天纪念日

    2024-07-09 22:16:09       20 阅读
  6. python 高级技巧 0706

    2024-07-09 22:16:09       16 阅读
  7. 前端面试基础html/js/css

    2024-07-09 22:16:09       17 阅读
  8. crontab定时任务不执行原因排查

    2024-07-09 22:16:09       16 阅读
  9. RTOS系统 -- ARM Cortex-M4 RPMSG之通道初始化函数

    2024-07-09 22:16:09       16 阅读
  10. shell中不常见的命令

    2024-07-09 22:16:09       20 阅读
  11. 直播APP开发源码搭建

    2024-07-09 22:16:09       17 阅读
  12. 自己写个简单的vite插件

    2024-07-09 22:16:09       23 阅读
  13. ROS melodic版本卸载---Ubuntu18.04

    2024-07-09 22:16:09       17 阅读
  14. Ubuntu手动编译源码安装Python

    2024-07-09 22:16:09       17 阅读
  15. [C++][CMake][生成可执行文件][下]详细讲解

    2024-07-09 22:16:09       20 阅读
  16. ubuntu防火墙指定端口开放设置

    2024-07-09 22:16:09       18 阅读