TestOverlay.ps1 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # WPF 覆盖字典切换机制实验(自包含、-STA 进程主线程直跑)
  2. $ErrorActionPreference = "Continue"
  3. try {
  4. Add-Type -AssemblyName PresentationFramework
  5. Add-Type -AssemblyName PresentationCore
  6. Add-Type -AssemblyName WindowsBase
  7. $base = "D:\Program\TeamAAS\ExE\win-x64"
  8. $hcDll = (Get-ChildItem $base -Recurse -Filter "HandyControl.dll" -ErrorAction SilentlyContinue | Select-Object -First 1).FullName
  9. Write-Output ("HC DLL: " + $hcDll)
  10. [void][System.Reflection.Assembly]::LoadFrom($hcDll)
  11. $app = New-Object System.Windows.Application
  12. $skinDark = New-Object System.Windows.ResourceDictionary
  13. $skinDark.Source = New-Object System.Uri("pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml")
  14. $theme = New-Object System.Windows.ResourceDictionary
  15. $theme.Source = New-Object System.Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
  16. [void]$app.Resources.MergedDictionaries.Add($skinDark)
  17. [void]$app.Resources.MergedDictionaries.Add($theme)
  18. $border = New-Object System.Windows.Controls.Border
  19. $border.SetResourceReference([System.Windows.Controls.Border]::BackgroundProperty, "RegionBrush")
  20. function Get-Bg { param($b)
  21. $brush = $b.GetValue([System.Windows.Controls.Border]::BackgroundProperty)
  22. if ($brush -is [System.Windows.Media.SolidColorBrush]) { return $brush.Color.ToString() }
  23. elseif ($null -eq $brush) { return "<null>" }
  24. else { return $brush.GetType().Name }
  25. }
  26. Write-Output ("1 初始(暗): " + (Get-Bg $border))
  27. # 第一次覆盖:Insert(0) 红色
  28. $ov1 = New-Object System.Windows.ResourceDictionary
  29. $ov1["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0xFF,0,0))
  30. $app.Resources.MergedDictionaries.Insert(0, $ov1)
  31. Write-Output ("2 Insert 红覆盖: " + (Get-Bg $border))
  32. # 第二次切换:Remove 旧 + Insert(0) 蓝色(模拟 UpdateTheme 的 Remove+Insert 序列)
  33. [void]$app.Resources.MergedDictionaries.Remove($ov1)
  34. $ov2 = New-Object System.Windows.ResourceDictionary
  35. $ov2["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0,0,0xFF))
  36. $app.Resources.MergedDictionaries.Insert(0, $ov2)
  37. Write-Output ("3 Remove+Insert 蓝覆盖: " + (Get-Bg $border))
  38. # 第三次:不 Remove 直接 Insert(0) 绿色
  39. $ov3 = New-Object System.Windows.ResourceDictionary
  40. $ov3["RegionBrush"] = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Color]::FromRgb(0,0xFF,0))
  41. $app.Resources.MergedDictionaries.Insert(0, $ov3)
  42. Write-Output ("4 再 Insert 绿覆盖: " + (Get-Bg $border))
  43. # 对照:移除全部覆盖回官方
  44. [void]$app.Resources.MergedDictionaries.Remove($ov3)
  45. [void]$app.Resources.MergedDictionaries.Remove($ov2)
  46. Write-Output ("5 移除全部覆盖: " + (Get-Bg $border))
  47. } catch {
  48. Write-Output ("EXCEPTION: " + $_.Exception.GetType().FullName + " :: " + $_.Exception.Message)
  49. if ($_.Exception.InnerException) { Write-Output ("INNER: " + $_.Exception.InnerException.Message) }
  50. }