C# WinForms 中嵌入 EXE 程序

在 C# WinForms 应用程序中,有时我们希望嵌入并控制其他 EXE 程序。这可以通过 Windows 提供的 API 来实现。本文将介绍如何在 WinForms 应用程序中嵌入并控制外部 EXE 程序。

什么是嵌入 EXE 程序

嵌入 EXE 程序是指在一个 WinForms 应用程序中启动并控制另一个 EXE 程序的过程。这可以通过设置子窗口来实现,其中子窗口承载外部程序的窗口。

实现步骤

1. 创建 WinForms 项目

首先,创建一个新的 WinForms 应用程序。

复制代码

shell

go 复制代码
dotnet new winforms -o EmbedExeApp
cd EmbedExeApp

2. 准备外部 EXE 程序

确保你有一个可以嵌入的外部 EXE 程序。这个程序可以是任何 Windows 应用程序,例如记事本、画图或其他自定义程序。

3. 使用 Windows API 嵌入 EXE

要嵌入外部 EXE,你需要使用一些 Windows API 函数,如 SetParent、ShowWindow 和 MoveWindow。这些函数可以通过 P/Invoke 在 C# 中调用。

复制代码

csharp

go 复制代码
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public partial class MainForm : Form
{
    private const int GWL_STYLE = -16;
    private const int WS_VISIBLE = 0x10000000;

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

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

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    public MainForm()
    {
        InitializeComponent();
    }

    private void EmbedProcess(string processPath)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo(processPath)
        {
            UseShellExecute = false,
            CreateNoWindow = true
        };

        Process process = Process.Start(startInfo);
        IntPtr childHandle = process.MainWindowHandle;
        SetParent(childHandle, this.Panel1.Handle);
        ShowWindow(childHandle, 1);
        MoveWindow(childHandle, 0, 0, this.Panel1.Width, this.Panel1.Height, true);
    }

    private void btnEmbedCalc_Click(object sender, EventArgs e)
    {
        EmbedProcess("calc.exe");
    }
}

4. 调用嵌入函数

在窗体中添加一个按钮,当点击按钮时,调用嵌入函数。

复制代码

csharp

go 复制代码
private void btnEmbedCalc_Click(object sender, EventArgs e)
{
    EmbedProcess("calc.exe"); // 嵌入计算器程序
}

5. 运行并测试

运行你的 WinForms 应用程序并点击按钮,你应该会看到计算器程序嵌入到你的窗体中。

注意事项

  • 确保在多线程环境下安全地访问 UI 控件。

  • 在调试和发布应用程序时,确保外部 EXE 文件的路径正确。

  • 嵌入的 EXE 程序应该与主应用程序兼容,特别是在 DPI 和窗口尺寸方面。

结论

通过上述步骤,我们可以在 WinForms 应用程序中实现类似 Android 的 Toast 消息提示效果。这种提示方式不会阻断用户操作,可以提供更流畅的用户体验。希望本文的示例代码能够帮助你在开发中实现这一功能。

往期精品推荐:

在国内默默无闻的.NET,在国外火的超乎想象?

C#的膨胀之路:创新还是灭亡

介绍.NET 6款好看的winform开源UI库

介绍一款最受欢迎的.NET 开源UI库

WPF第三方开源UI框架:打造独特体验的魔法师

WPF与Winform,你的选择是?

WinForm的前世今生

.NET成年了,然后呢?------编程界的逆袭传奇

相关推荐
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹5 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁5 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
孙启超5 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int5 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger5 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读5 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json