Created
December 29, 2020 12:26
-
-
Save COFFEETALES/0a8710a9b37f1c21972b5a3f2b771315 to your computer and use it in GitHub Desktop.
PowerShell, WinForms and EdgeHTML control
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' )) | |
) | |
Start-Process -Wait -FilePath $WindowsPowershellPath ` | |
-ArgumentList ( | |
'-NoLogo', | |
'-NoProfile', | |
'-NonInteractive', | |
'-ExecutionPolicy', 'ByPass', | |
'-File', ('"', ($MyInvocation.MyCommand.Definition), '"' -join ''), | |
$URL, | |
'EdgeHTML' | |
) | |
} | |
If ( 'EdgeHTML' -ine $Mode ) { | |
Return | |
} | |
Try { | |
# https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/tree/master/Microsoft.Toolkit.Forms.UI.Controls.WebView | |
# https://www.nuget.org/packages/Microsoft.Toolkit.Forms.UI.Controls.WebView | |
# https://docs.microsoft.com/en-us/dotnet/api/microsoft.toolkit.forms.ui.controls.webview?view=win-comm-toolkit-dotnet-stable | |
$ErrorActionPreference = 'Stop'; | |
$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 -eq $PartialName }) ) | |
{ [Void][Reflection.Assembly]::LoadWithPartialName($PartialName) } | |
} | |
& $LoadAssembly 'System.Windows.Forms' | |
& $LoadAssembly 'System.Drawing' | |
If ( -not ([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -eq 'Microsoft.Toolkit.Forms.UI.Controls.WebView' }) ) | |
{ | |
[Void][Reflection.Assembly]::LoadFrom( | |
'C:\programs\EdgeHTMLWebView\microsoft.toolkit.forms.ui.controls.webview.6.1.2\lib\net462\Microsoft.Toolkit.Forms.UI.Controls.WebView.dll' | |
) | |
[Void][Reflection.Assembly]::LoadWithPartialName('Microsoft.Toolkit.Forms.UI.Controls.WebView') | |
} | |
[Windows.Forms.Application]::EnableVisualStyles() | |
$MainForm = [Windows.Forms.Form]::New() | |
$MainForm.FormBorderStyle = [Windows.Forms.FormBorderStyle]::Sizable | |
$MainForm.MaximizeBox = $TRUE | |
$MainForm.MinimizeBox = $TRUE | |
$MainForm.Text = 'WebView' | |
$MainForm.ClientSize = [Drawing.Size]::New(800, 600) | |
$MainForm.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen | |
$MainForm.AutoScalemode = [Windows.Forms.AutoScaleMode]::None | |
$WebView = [Microsoft.Toolkit.Forms.UI.Controls.WebView]::New() | |
( [ComponentModel.ISupportInitialize]$WebView ).BeginInit() | |
$WebView.ClientSize = [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 ) | |
$WebView.Navigate( ($URL) ) | |
#[Void]$MainForm.ShowDialog() | |
[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