WinForm使用WebView2开发现代应用

WinForm使用WebView2开发现代应用

在微信公众号中看到可以使用WinForm来构建Web应用程序,于是尝试自己制作一个简单的应用。

在WinForm中主要是使用WebView2控件来构建的。

第一步:新建WinForm程序,布局界面如下:

在这里插入图片描述

第二步:使用NuGet包管理工具下载WebView2控件

如下图:

微信截图_20250216224839

笔者项目已经安装完了,直接在工具箱中使用即可。

上面是一个TextBox控件,和一个按钮;TetxBox控件填写网址,点击按钮访问相应的网址。

第三步:编写代码。如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;

namespace WebViewTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Resize += new System.EventHandler(this.Form1_Resize);

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            webView.Size = this.ClientSize - new System.Drawing.Size(webView.Location);
            button1.Left = this.ClientSize.Width - button1.Width;
            textBox1.Width = button1.Left - textBox1.Left;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (webView != null && webView.CoreWebView2 != null)
            {
                if (string.IsNullOrEmpty(textBox1.Text) == false)
                {
                    webView.CoreWebView2.Navigate(textBox1.Text);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form1_Resize(sender,e);
        }
    }
}

下面是输入百度网址后的运行效果:

微信截图_20250216225640

参考文献:

1.WinForms 应用中的 WebView2 入门

好了,在WinForm中简单使用WebView2控件的介绍就到这里了。关于WebView2控件的更详细是使用方式,大家可以参考官方文档。

WebView2 documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值