Last active
October 22, 2025 08:35
-
-
Save COFFEETALES/f2090756c1581036d68da79730907b19 to your computer and use it in GitHub Desktop.
PowerShell, Winforms and WebView 2, the getting started app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Param ( [String]$URL = 'https://coffeetales.net', [String]$Mode = 'Default' ) | |
| If ( 'Default' -ieq $Mode ) { | |
| #[String]$WindowsPowershellPath = | |
| # [IO.Path]::Combine( | |
| # ([String[]]@( [Environment]::SystemDirectory, 'WindowsPowerShell', 'v1.0', 'powershell.exe' )) | |
| # ) | |
| [String]$PowerShellPath = Get-Process -Id $PID | Select-Object -ExpandProperty Path | |
| Start-Process ` | |
| -Wait ` | |
| -FilePath $PowerShellPath ` | |
| -NoNewWindow ` | |
| -ArgumentList ( | |
| '-NoLogo', | |
| '-NoProfile', | |
| '-NonInteractive', | |
| #'-WindowStyle Hidden', | |
| #'-NoExit', | |
| '-ExecutionPolicy', 'ByPass', | |
| '-File', ('"', ($MyInvocation.MyCommand.Definition), '"' -join ''), | |
| $URL, | |
| 'EdgeChromium' | |
| ) | |
| } | |
| If ( 'EdgeChromium' -ine $Mode ) { | |
| Return | |
| } | |
| Try { | |
| # https://github.com/MicrosoftEdge/WebView2Samples/tree/master/SampleApps/WebView2WindowsFormsBrowser | |
| # https://www.nuget.org/packages/Microsoft.Web.WebView2 | |
| # https://docs.microsoft.com/en-us/microsoft-edge/webview2/webview2-api-reference | |
| $ErrorActionPreference = 'Stop' | |
| [Byte]$MODE_FULLSCREEN = 0 | |
| $SetProcessDPIAware = | |
| Add-Type -PassThru -Name User32SetProcessDPIAware ` | |
| -MemberDefinition ( | |
| @' | |
| [System.Runtime.InteropServices.DllImport("user32.dll")] | |
| public static extern bool SetProcessDPIAware(); | |
| '@ | |
| ) | |
| [Void]$SetProcessDPIAware::SetProcessDPIAware() | |
| [ScriptBlock]$LoadAssembly = { | |
| Param ( [String]$PartialName ) | |
| If ( -not ([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -ieq $PartialName }) ) | |
| { [Void][Reflection.Assembly]::LoadWithPartialName($PartialName) } | |
| } | |
| & $LoadAssembly 'System.Windows.Forms' | |
| & $LoadAssembly 'System.Drawing' | |
| [Windows.Forms.FormClosingEventHandler]$CloseHandler = | |
| [Windows.Forms.FormClosingEventHandler]{ | |
| If ( [Windows.Forms.CloseReason]::UserClosing -ceq $_.CloseReason ) | |
| { $_.Cancel = $TRUE } | |
| } | |
| # $CloseHandler.GetInvocationList().Length | |
| # ^ ne semble pas donner de résultat concluant. | |
| [ScriptBlock]$ProcessFullScreen = { | |
| [Drawing.Rectangle]$ScreenRect = [Windows.Forms.Screen]::GetBounds($MainForm) | |
| $MainForm.Remove_FormClosing($CloseHandler) | |
| If ( 0 -cne $MODE_FULLSCREEN ) { | |
| $MainForm.WindowState = [Windows.Forms.FormWindowState]::Normal | |
| $MainForm.MaximizeBox = $FALSE | |
| $MainForm.MinimizeBox = $FALSE | |
| $MainForm.FormBorderStyle = [Windows.Forms.FormBorderStyle]::None | |
| $MainForm.Bounds = $ScreenRect | |
| $WebView.Size = [Drawing.Size]::New($ScreenRect.Width, $ScreenRect.Height) | |
| $MainForm.Add_FormClosing($CloseHandler) | |
| } | |
| Else { | |
| $MainForm.WindowState = [Windows.Forms.FormWindowState]::Normal | |
| $MainForm.MaximizeBox = $TRUE | |
| $MainForm.MinimizeBox = $TRUE | |
| $MainForm.FormBorderStyle = [Windows.Forms.FormBorderStyle]::Sizable | |
| [Int32[]]$MySize = @( ($ScreenRect.Width * 0.7), ($ScreenRect.Height * 0.7) ) | |
| $MainForm.ClientSize = [Drawing.Size]::New($MySize[0], $MySize[1]) | |
| $WebView.Size = [Drawing.Size]::New($MySize[0], $MySize[1]) | |
| $MainForm.Location = [Drawing.Point]::New( | |
| $ScreenRect.Width * 0.5 - $MainForm.Width * 0.5, | |
| $ScreenRect.Height * 0.5 - $MainForm.Height * 0.5 | |
| ) | |
| } | |
| } | |
| $Env:PATH = ('C:\programs\WebView2\runtimes\win-x64\native', $Env:PATH -join ';') | |
| If ( -not ([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -ieq 'Microsoft.Web.WebView2.WinForms.WebView2' }) ) | |
| { | |
| If ( $TRUE -ceq (Test-Path -LiteralPath Variable:\IsWindows -PathType Leaf) ) { | |
| [Void][Reflection.Assembly]::LoadFrom('C:\programs\WebView2\lib\netcoreapp3.0\Microsoft.Web.WebView2.Core.dll') | |
| [Void][Reflection.Assembly]::LoadFrom('C:\programs\WebView2\lib\netcoreapp3.0\Microsoft.Web.WebView2.WinForms.dll') | |
| } | |
| Else { | |
| [Void][Reflection.Assembly]::LoadFrom('C:\programs\WebView2\lib\net45\Microsoft.Web.WebView2.Core.dll') | |
| [Void][Reflection.Assembly]::LoadFrom('C:\programs\WebView2\lib\net45\Microsoft.Web.WebView2.WinForms.dll') | |
| } | |
| #[Void][Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.WebView2.WinForms.WebView2') | |
| } | |
| [Windows.Forms.Application]::EnableVisualStyles() | |
| [Windows.Forms.Application]::SetCompatibleTextRenderingDefault($FALSE) | |
| #[Void][Windows.Forms.Application]::SetHighDpiMode( [System.Windows.Forms.HighDpiMode]::PerMonitorV2 ) | |
| $MainForm = [Windows.Forms.Form]::New() | |
| $MainForm.WindowState = [Windows.Forms.FormWindowState]::Normal | |
| $MainForm.Text = 'CoffeeTales.NET' | |
| $MainForm.ClientSize = [Drawing.Size]::New(800, 600) | |
| $MainForm.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen | |
| $MainForm.AutoScalemode = [Windows.Forms.AutoScaleMode]::None | |
| $MainForm.SuspendLayout() | |
| $WebView = [Microsoft.Web.WebView2.WinForms.WebView2]::New() | |
| ( [ComponentModel.ISupportInitialize]$WebView ).BeginInit() | |
| $WebView.Name = 'WebView2' | |
| $WebView.Size = [Drawing.Size]::New(800, 600) | |
| $WebView.Anchor = | |
| [Windows.Forms.AnchorStyles]::Top -bor | |
| [Windows.Forms.AnchorStyles]::Right -bor | |
| [Windows.Forms.AnchorStyles]::Bottom -bor | |
| [Windows.Forms.AnchorStyles]::Left | |
| ( [ComponentModel.ISupportInitialize]$WebView ).EndInit() | |
| $MainForm.Controls.Add( $WebView ) | |
| $MainForm.ResumeLayout($FALSE) | |
| $MainForm.PerformLayout() | |
| $WebView2Options = [Microsoft.Web.WebView2.Core.CoreWebView2EnvironmentOptions]::New() | |
| $WebView2Env = [Microsoft.Web.WebView2.Core.CoreWebView2Environment]::CreateAsync( | |
| [String]::Empty, [IO.Path]::Combine( [String[]]([IO.Path]::GetTempPath(), 'CoffeeTalesWebView') ), $WebView2Options | |
| ) | |
| $WebView2Env.GetAwaiter().OnCompleted( | |
| [Action]{ | |
| $WebView.EnsureCoreWebView2Async( $WebView2Env.Result ) | |
| $WebView.Source = [Uri]::New( $URL ) | |
| } | |
| ) | |
| $WebView.Add_NavigationCompleted( | |
| [EventHandler[Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs]]{ | |
| #$WebView.Focus() | |
| } | |
| ) | |
| $WebView.Add_CoreWebView2InitializationCompleted( | |
| [EventHandler[Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs]]{ | |
| #$WebView.CoreWebView2.Settings | gm | out-host | |
| $MainForm.Add_Activated([EventHandler]{ If ( 0 -cne $MODE_FULLSCREEN ) { $MainForm.Add_FormClosing($CloseHandler) } }) | |
| $MainForm.Add_Deactivate([EventHandler]{ $MainForm.Remove_FormClosing($CloseHandler) }) | |
| #& $ProcessNoDevTools | |
| If ( $TRUE -ceq (Test-Path -LiteralPath 'Env:\CALIDEV' -PathType Leaf) ) | |
| { Return } | |
| [Microsoft.Web.WebView2.Core.CoreWebView2Settings]$Settings = $WebView.CoreWebView2.Settings | |
| $Settings.AreDefaultContextMenusEnabled = $FALSE | |
| $Settings.AreDefaultScriptDialogsEnabled = $FALSE | |
| $Settings.AreDevToolsEnabled = $FALSE | |
| $Settings.AreHostObjectsAllowed = $FALSE | |
| $Settings.IsBuiltInErrorPageEnabled = $FALSE | |
| $Settings.IsScriptEnabled = $TRUE | |
| $Settings.IsStatusBarEnabled = $FALSE | |
| $Settings.IsWebMessageEnabled = $TRUE | |
| $Settings.IsZoomControlEnabled = $FALSE | |
| } | |
| ) | |
| $WebView.Add_KeyDown( | |
| [Windows.Forms.KeyEventHandler]{ | |
| If ( [Windows.Forms.Keys]::F2 -ceq $_.KeyData ) | |
| { | |
| Set-Variable -Scope 1 -Name 'MODE_FULLSCREEN' -Value ($MODE_FULLSCREEN -bxor 1) | |
| & $ProcessFullScreen | |
| $_.Handled = $TRUE | |
| $_.SuppressKeyPress = $TRUE | |
| } | |
| } | |
| ) | |
| $MainForm.Add_Load([EventHandler]{ <#& $ProcessFullScreen#> }) | |
| [Windows.Forms.Application]::Run( $MainForm ) | |
| } | |
| Finally { | |
| If ( $NULL -cne $WebView ) { | |
| #$WebView.Close() | |
| $WebView.Dispose() | |
| $WebView = $NULL | |
| } | |
| If ( $NULL -cne $MainForm ) { | |
| $MainForm.Dispose() | |
| $MainForm = $NULL | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment