C#Winform窗体中嵌入exe文件

news/2024/7/8 7:53:42 标签: c#, 开发语言

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);
    }


http://www.niftyadmin.cn/n/5536864.html

相关文章

c进阶篇(四):内存函数

内存函数以字节为单位更改 1.memcpy memcpy 是 C/C 中的一个标准库函数&#xff0c;用于内存拷贝操作。它的原型通常定义在 <cstring> 头文件中&#xff0c;其作用是将一块内存中的数据复制到另一块内存中。 函数原型&#xff1a;void *memcpy(void *dest, const void…

ffmpeg使用mjpeg把yuvj420p编码为jpg图像

version #define LIBAVCODEC_VERSION_MAJOR 60 #define LIBAVCODEC_VERSION_MINOR 15 #define LIBAVCODEC_VERSION_MICRO 100 note 1. 通过*.jpg推测时&#xff0c;out_fmt为image2&#xff0c;打开*.jpg文件时&#xff0c;in_fmt为image2 但是out_fmt为image2时&…

中英双语介绍美国的州:阿肯色州(Arkansas)

中文版 阿肯色州简介 阿肯色州&#xff08;Arkansas&#xff09;位于美国南部&#xff0c;以其多样的自然景观、丰富的文化遗产和不断发展的经济而闻名。以下是对阿肯色州的详细介绍&#xff0c;包括其地理位置、人口、经济、教育、文化和主要城市。 地理位置 阿肯色州东临…

【经验总结】Springboot打印指定类的日志到指定文件中

原文地址&#xff1a;https://www.cnblogs.com/zeng1994/p/f9bff238b13a0bf8fb8bf88c41db7a34.html 以下内容已经过实践&#xff0c;勘误&#xff0c;总结 环境&#xff1a;Springboot2.5.2 公司有个项目&#xff0c;需要和几个第三方系统对接。这种项目&#xff0c;日志一定要…

C++ | Leetcode C++题解之第214题最短回文串

题目&#xff1a; 题解&#xff1a; class Solution { public:string shortestPalindrome(string s) {int n s.size();vector<int> fail(n, -1);for (int i 1; i < n; i) {int j fail[i - 1];while (j ! -1 && s[j 1] ! s[i]) {j fail[j];}if (s[j 1] …

Vbus 和 Vbat

在嵌入式系统开发中&#xff0c;Vbus 和 Vbat 是两个不同的电源相关术语&#xff0c;它们的区别主要在于它们的用途和连接的电源类型。 Vbus 定义: Vbus 通常是指 USB 总线电压。在 USB 2.0 中&#xff0c;Vbus 通常为 5V 电源。用途: Vbus 提供电源给 USB 设备&#xff0c;确…

3.用户程序与驱动交互

驱动程序请使用第二章https://blog.csdn.net/chenhequanlalala/article/details/140034424 用户app与驱动交互最常见的做法是insmod驱动后&#xff0c;生成一个设备节点&#xff0c;app通过open&#xff0c;read等系统调用去操作这个设备节点&#xff0c;这里先用mknode命令调…

arm_uart4实验

#include "uart4.h" //UART //初始化 void hal_uart4_init() { //rcc_init //…