環境
- Windows 11 バージョン24H2(26100.3915)
- .NET Framework 4.7.2
- メモ帳
NuGetからWebView2のDLLを準備
-
About
>Download package
-
.nupkg
を.zip
に書き換えて展開 -
lib
とかbuild
の中にDLLがいる -
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