详解C# wpf如何嵌入外部程序

 更新时间:2024年04月09日 11:25:10   作者:CodeOfCC  
实现嵌入各种窗口控件后,其实还会有一种需求:嵌入外部程序,我们有时可能需要嵌入一个浏览器或者或者播放器等一些已有的程序,下面我们就来看看具体操作吧

前言

实现嵌入各种窗口控件后,其实还会有一种需求:嵌入外部程序,我们有时可能需要嵌入一个浏览器或者或者播放器等一些已有的程序,其嵌入原理也和前面差不多,只要能获取进程的主窗口句柄,然后将窗口嵌入。

一、如何实现

1、定义属性

定义一个依赖属性,提供给xaml设置进程运行的命令行

public class AppHost : HwndHost
{
    /// <summary>
    /// 进程运行的命令行
    /// </summary>
    public string Cmdline
    {
        get { return (string)GetValue(CmdlineProperty); }
        set { SetValue(CmdlineProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Cmdline.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CmdlineProperty =
        DependencyProperty.Register("Cmdline", typeof(string), typeof(AppHost), new PropertyMetadata(""));
}        

2、进程嵌入

在下列方法中进行进程嵌入,具体操作如下列步骤。

protected override HandleRef BuildWindowCore(HandleRef hwndParent)

(1)启动进程

var cmds = Cmdline.Split(" ", 2);
Process? _process;
_process.StartInfo.FileName = cmds.First();
_process.StartInfo.Arguments = cmds.Last();
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
_process.Start();

(2)进程加入作业对象

这个步骤是用于管理进程,确保《子进程跟随主进程关闭》

static Job _job = new Job();
_job.AddProcess(_process.Handle);

(3)获取主窗口句柄

下列提供的是简单获取主窗口句柄的方法。通过延时等待的方式获取。需要精确时间获取主窗口句柄则可以使用钩子,在子进程窗口创建事件中获取句柄。

for (int i = 0; i < 200 && _process.MainWindowHandle == 0; i++) Thread.Sleep(5);
if (_process.MainWindowHandle == 0)
{
    throw new Exception("process no window");
}
return new HandleRef(this, Handle);

3、销毁进程

protected override void DestroyWindowCore(HandleRef hwnd)
{
    _process?.Kill();
    _process?.Dispose();
    _process = null;
}

二、完整代码

其中Job对象在《子进程跟随主进程关闭》中。

AppHost.cs

using JobManagement;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using Process = System.Diagnostics.Process;
using TextBox = System.Windows.Controls.TextBox;
using Thread = System.Threading.Thread;

namespace WpfHwndElement
{
    /// <summary>
    /// 需要手动dispose此控件。
    /// </summary>
    public class AppHost : HwndHost
    {
        static Job _job = new Job();
        Process? _process;
        /// <summary>
        /// 进程运行的命令行
        /// </summary>
        public string Cmdline
        {
            get { return (string)GetValue(CmdlineProperty); }
            set { SetValue(CmdlineProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Cmdline.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CmdlineProperty =
            DependencyProperty.Register("Cmdline", typeof(string), typeof(AppHost), new PropertyMetadata(""));

        new public IntPtr Handle
        {
            get { return (IntPtr)GetValue(HandleProperty); }
            private set { SetValue(HandleProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Hwnd.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HandleProperty =
            DependencyProperty.Register("Handle", typeof(IntPtr), typeof(NativeHost), new PropertyMetadata(IntPtr.Zero));
        protected override HandleRef BuildWindowCore(HandleRef hwndParent)
        {
            try
            {
                if (DesignerProperties.GetIsInDesignMode(this)) throw new Exception("design mode won't show app");
                var cmds = Cmdline.Split(" ", 2);
                _process = new Process();
                _process.StartInfo.FileName = cmds.First();
                _process.StartInfo.Arguments = cmds.Length > 1 ? cmds.Last() : "";
                _process.StartInfo.UseShellExecute = false;
                _process.StartInfo.CreateNoWindow = true;
                _process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
                _process.Start();
                _job.AddProcess(_process.Handle);
                for (int i = 0; i < 200 && _process.MainWindowHandle == 0; i++) Thread.Sleep(5);
                if (_process.MainWindowHandle == 0)
                {
                    throw new Exception("process no window");
                }
                Handle = _process.MainWindowHandle;
                var wndStyle = GetWindowLong(Handle, GWL_STYLE);
                wndStyle &= ~WS_THICKFRAME;
                wndStyle &= ~WS_CAPTION;
                SetWindowLong(Handle, GWL_STYLE, wndStyle | WS_CHILD);
                SetParent(Handle, hwndParent.Handle);
            }
            catch (Exception ex)
            {
                var window = new Window() { Width = 0, Height = 0, ResizeMode = ResizeMode.NoResize, WindowStyle = WindowStyle.None, Content = new TextBox() { IsReadOnly = true, Text = ex.Message + " " + ex.StackTrace, TextWrapping = TextWrapping.Wrap } };
                var hwnd = new WindowInteropHelper(window).EnsureHandle();
                window.Show();
                SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_CHILD);
                SetParent(hwnd, hwndParent.Handle);
                Handle = hwnd;
            }
            return new HandleRef(this, Handle);
        }
        protected override void DestroyWindowCore(HandleRef hwnd)
        {
            var window = HwndSource.FromHwnd(hwnd.Handle)?.RootVisual as Window;
            window?.Close();
            _process?.Kill();
            _process?.Dispose();
            _process = null;
        }
        const int WS_CAPTION = 0x00C00000;
        const int WS_THICKFRAME = 0x00040000;
        const int WS_CHILD = 0x40000000;
        const int GWL_STYLE = (-16);
        [DllImport("user32.dll", EntryPoint = "GetWindowLongW")]
        static extern int GetWindowLong(IntPtr hwnd, int nIndex);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongW")]
        static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    }
}

三、使用示例

嵌入ffplay.exe

MainWindow.xaml

<Window x:Class="WpfHwndElement.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfHwndElement"
        mc:Ignorable="d"
        Title="MainWindow" Height="360" Width="640"       
        >
    <Grid>
        <local:AppHost Cmdline="ffplay" Width="200" Height="200"></local:AppHost>
    </Grid>
</Window>

效果预览

在这里插入图片描述

总结

嵌入外部程序还是相对比较容易实现的,而且也有一定的使用场景。创建进程,并能获取到进程的主窗口句柄即可。另外要注意的是管理子进程的退出,其他都问题不大。

以上就是详解C# wpf如何嵌入外部程序的详细内容,更多关于C# wpf嵌入外部程序的资料请关注脚本之家其它相关文章!

相关文章

  • C#面向对象设计原则之接口隔离原则

    C#面向对象设计原则之接口隔离原则

    这篇文章介绍了C#面向对象设计原则之接口隔离原则,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C# 通过同步和异步实现优化做早餐的时间

    C# 通过同步和异步实现优化做早餐的时间

    本文以一个简单的小例子—如何做一顿早餐及如何优化做早餐的时间来让大家具体了解一下同步和异步方法的区别,需要的朋友可以参考一下
    2021-12-12
  • C#面向对象特征的具体实现及作用详解

    C#面向对象特征的具体实现及作用详解

    所有的面相对象思想,归根结底是为了简化代码,减少代码量,构建更符合现实生活逻辑的程序代码,从而减轻程序员的负担。不能一味地或者说刻意地去使用面相对象的思想而忽略了程序所实现的功能或者框架,要根据实际情况
    2013-10-10
  • unity实现简单抽奖系统

    unity实现简单抽奖系统

    这篇文章主要为大家详细介绍了unity实现简单抽奖系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • WPF实现列表分页控件的示例代码

    WPF实现列表分页控件的示例代码

    这篇文章主要为大家详细介绍了如何利用WPF实现列表分页控件,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2022-10-10
  • Unity实现轮盘方式的按钮滚动效果

    Unity实现轮盘方式的按钮滚动效果

    这篇文章主要为大家详细介绍了Unity实现轮盘方式的按钮滚动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C#开发者必备的PDF文档高效处理操作指南

    C#开发者必备的PDF文档高效处理操作指南

    PDF作为一种通用且可靠的文档格式,在商业和日常工作中扮演着举足轻重的角色,本文旨在为C#开发者提供一份详尽的教程,深入探讨如何利用一套成熟的.NET库来编辑PDF,希望对大家有所帮助
    2025-11-11
  • C# PictureBox控件方法参数及图片删除重命名上传详解

    C# PictureBox控件方法参数及图片删除重命名上传详解

    这篇文章主要为大家介绍了C# PictureBox控件方法参数及图片删除重命名上传示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • C#图书管理系统 附源码下载

    C#图书管理系统 附源码下载

    这篇文章主要为大家详细介绍了C#图书管理系统,文章中附源码下载,示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • C# 委托的三种调用示例(同步调用 异步调用 异步回调)

    C# 委托的三种调用示例(同步调用 异步调用 异步回调)

    本文将主要通过同步调用、异步调用、异步回调三个示例来讲解在用委托执行同一个加法类的时候的的区别和利弊
    2013-12-12

最新评论