用AI生成一个简单的视频剪辑工具 的后续

起因

有网友问俺 如何在winform中使用AI 生成的ui。

回答

Microsoft.Web.WebView2,用着个显示AI给出的html的UI

在nuget里安装这个包,就ok。

使用说明

俺的习惯是把WebView2 放在用户控件中,然后再加个互动的自定义事件。

public delegate void CustomEventHandler(object sender, CustomEventArgs e);

// 定义一个自定义事件

public event CustomEventHandler customEvent;

通过 webView21.CoreWebView2.AddHostObjectToScript("customHost", customHost);

绑定到C#端。

俺加放了和 标签 显示"正在加载",然后在 ContentLoading 时 Visible = false

private void webView21_ContentLoading(object sender, Microsoft.Web.WebView2.Core.CoreWebView2ContentLoadingEventArgs e)

{

if (label1.Visible)

label1.Visible = false;

}

在使用的时候 ,自己使用这个 用户控件 就ok了

使用时在窗体的Shown 加载html,并绑定事件customEvent

private void Form_VideoCut_Shown(object sender, EventArgs e)

{

userControl_HtmlView1.customEvent += onCustom;

string dir = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

string fn = System.IO.Path.Combine(dir, "html", "videocut.htm");

userControl_HtmlView1.load_file(fn);

}

在事件中,完成功能:

public void onCustom(object sender, CustomEventArgs e)

{

if (e.message == "get_video_url")

{

Dictionary<string, string> dict = new Dictionary<string, string>();

dict["video_url"] = "file://"+filename;

e.result = Newtonsoft.Json.JsonConvert.SerializeObject(dict);

}

if (e.message == "close")

{

Close();

}

if (e.message.StartsWith("proc:"))

{

clear_tmp();

string s = e.message.Substring("proc:".Length);

proc(s.Trim());

return;

}

完整代码

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XXXXTools
{
    public partial class UserControl_HtmlView : UserControl
    {
        public UserControl_HtmlView()
        {
            InitializeComponent();
        }
        public string Proc(string message)
        {
            if (customEvent != null)
            {
                CustomEventArgs e = new CustomEventArgs(message);
                customEvent(this,e);
                return e.result;
            }
            return "{}";
        }
        public delegate void CustomEventHandler(object sender, CustomEventArgs e);

        // 定义一个自定义事件
        public event CustomEventHandler customEvent;

         
        public bool EnsureCoreWebView2 = false;
        public CustomHost customHost= null;
        public async void load_html(string htm)
        {
            if (!EnsureCoreWebView2)
            {
                await webView21.EnsureCoreWebView2Async();
                EnsureCoreWebView2 = true;
            }
            if (customHost == null)
            {
                customHost = new CustomHost();
                customHost.set_control(this);
                webView21.CoreWebView2.AddHostObjectToScript("customHost", customHost);
                
            }
            webView21.NavigateToString(htm);
            
        }

        public async void load_file(string fn)
        {
            if (!EnsureCoreWebView2)
            {
                await webView21.EnsureCoreWebView2Async();
                EnsureCoreWebView2 = true;
            }
            if (customHost == null)
            {
                customHost = new CustomHost();
                customHost.set_control(this);
                webView21.CoreWebView2.AddHostObjectToScript("customHost", customHost);
                
            }
            webView21.CoreWebView2.Navigate(fn);
            
        }

        private void UserControl_HtmlView_Resize(object sender, EventArgs e)
        {
            label1.Left = (this.ClientSize.Width - label1.Width) / 2;
            label1.Top = (this.ClientSize.Height - label1.Height) / 2;
        }

        private void webView21_LocationChanged(object sender, EventArgs e)
        {
           
        }

        private void webView21_ContentLoading(object sender, Microsoft.Web.WebView2.Core.CoreWebView2ContentLoadingEventArgs e)
        {
            if (label1.Visible)
                label1.Visible = false;
        }
    }

    [ComVisible(true)]
    public class CustomHost
    {
        public string Proc(string message)
        {            
            if (control != null)
            {
                UserControl_HtmlView c = (UserControl_HtmlView)control;
                return c.Proc(message);
            } 
            return "{}";
        }
        public void set_control(object obj)
        {
            control = obj;
        }
        private Object control;
      
    }

    public class CustomEventArgs : EventArgs
    {
        public string message { get; private set; }
        public string result { get;  set; }

        public CustomEventArgs(string msg)
        {
            message = msg;
        }
    }

}
相关推荐
曹牧6 小时前
C#:Obsolete
开发语言·c#
我是苏苏6 小时前
Web开发:使用C#的System.Drawing.Common将png图片转化为icon图片
开发语言·c#
阿蒙Amon6 小时前
C#每日面试题-is和as的区别
java·开发语言·c#
阿蒙Amon7 小时前
C#每日面试题-简述泛型约束
java·开发语言·c#
1314lay_10078 小时前
C# .Net 7.0 Core添加日志可视化
visualstudio·c#·.net·.netcore
gc_22999 小时前
学习C#调用OpenXml操作word文档的基本用法(17:学习文档图片类)
c#·word·图片·openxml
LcVong10 小时前
C# 基于MemoryMappedFile实现进程间通信(服务端+客户端完整范例)
linux·服务器·c#
时光追逐者10 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 66 期(2026年1.12-1.18)
c#·.net·.netcore
一个帅气昵称啊10 小时前
.Net C# AI 如何实现联网搜索
人工智能·c#·.net
为你写首诗ge11 小时前
【WebApi】C#创建WebApi学习
c#·web api