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

}
相关推荐
钰fly2 小时前
Windows Forms开发工具与功能总结表
前端·c#
lzhdim2 小时前
C#性能优化:从入门到入土!这10个隐藏技巧让你的代码快如闪电
开发语言·性能优化·c#
=PNZ=BeijingL3 小时前
SprintBoot +Screw+PostgreSQL生成数据库文档时空指针问题
开发语言·c#
Space-Junk3 小时前
C#描述-计算机视觉OpenCV(8):OCR字符检测
opencv·计算机视觉·c#
kevin_水滴石穿3 小时前
C#获取程序集和文件版本
开发语言·c#
flysh054 小时前
C#和.NET简介
开发语言·c#·.net
月巴月巴白勺合鸟月半5 小时前
用AI生成一个简单的视频剪辑工具
人工智能·c#
唐青枫5 小时前
深入理解 Parallel.ForEachAsync:C#.NET 并行调度模型揭秘
c#·.net
cici1587414 小时前
C#实现三菱PLC通信
java·网络·c#