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

}
相关推荐
缺点内向22 分钟前
C#: 告别繁琐!轻松移除Word文档中的文本与图片水印
c#·自动化·word·.net
喵叔哟1 小时前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
2501_930707781 小时前
使用 C# .NET 从 PowerPoint 演示文稿中提取背景图片
c#·powerpoint·.net
初级代码游戏2 小时前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大空大地20263 小时前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
kylezhao20195 小时前
C#序列化与反序列化详细讲解与应用
c#
JQLvopkk5 小时前
C# 实践AI :Visual Studio + VSCode 组合方案
人工智能·c#·visual studio
故事不长丨5 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
kingwebo'sZone5 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
大空大地20266 小时前
表达式与运算符
c#