0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】WebView2テスト

Posted at

環境

  • Windows 11 バージョン24H2(26100.3915)
  • .NET Framework 4.7.2
  • メモ帳

NuGetからWebView2のDLLを準備

  1. About > Download package
  2. .nupkg.zipに書き換えて展開
  3. libとかbuildの中にDLLがいる
  4. C:\Windows\Microsoft.NET\Framework\v4.0.30319に以下のDLLをコピーする
  • Microsoft.Web.WebView2.Core.dll
  • Microsoft.Web.WebView2.WinForms.dll
  • WebView2Loader.dll

csを書く

WebView2_App.cs
using System;
using System.Windows.Forms;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;

class WebViewForm : Form
{
    private WebView2 webView;

    public WebViewForm()
    {
        this.Width = 1000;
        this.Height = 700;

        webView = new WebView2();
        webView.Dock = DockStyle.Fill;
        this.Controls.Add(webView);

        this.Load += async (s, e) =>
        {
            await webView.EnsureCoreWebView2Async();
            webView.CoreWebView2.Navigate("http://www.yamauchifumito.com/#");

            // ページ読み込み
            webView.CoreWebView2.NavigationCompleted += async (sender, args) =>
            {
                // 何か処理
            };
        };
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new WebViewForm());
    }
}

ビルドする

build.bat
@echo off
setlocal

set CSC=%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe

set OUT=webview_app.exe

%CSC% /target:winexe /out:%OUT% ^
    /reference:"Microsoft.Web.WebView2.WinForms.dll" ^
    /reference:"Microsoft.Web.WebView2.Core.dll" ^
    /reference:"System.Windows.Forms.dll" ^
    /reference:"System.Drawing.dll" ^
    webview.cs

if exist %OUT% (
    echo.
    echo === ビルドできてるよ ===
) else (
    echo.
    echo !!! ビルドできてないよ !!!
)
endlocal
pause
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?