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

}
相关推荐
程序设计实验室33 分钟前
Spark.NET:一个试图把 Django / Rails 式开发体验带回 .NET 世界的全栈 Web 框架。
c#
byoass2 小时前
智巢AI知识库深度解析:企业文档管理从大海捞针到精准狙击的进化之路
开发语言·网络·人工智能·安全·c#·云计算
njsgcs6 小时前
solidworks自动标注折弯4 无向图 c#
开发语言·c#·solidworks
我是唐青枫7 小时前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net
JQLvopkk8 小时前
C# 工业级上位机:交互实战
开发语言·c#·交互
kingwebo'sZone9 小时前
PdfiumViewer使用权限控制期操作按钮(PdfViewer其实也可以完整兼容)
c#
张小俊_10 小时前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
無斜10 小时前
【CAPL实用开发】--- CAPL调用 .NET DLL
开发语言·c#·capl·canoe
puamac11 小时前
UcTabWindow 布局多tab,加载编辑器和资源管理器等自定义控件
c#·编辑器·datagridview
唐青枫11 小时前
别再把增删改查写成一锅粥!C#.NET CQRS 从原理到实战
c#·.net