Board logo

标题: [问题求助] PowerShell怎样根据程序名设置窗口半透明效果 [打印本页]

作者: 5i365    时间: 2021-12-13 19:19     标题: PowerShell怎样根据程序名设置窗口半透明效果

找了一段代码, 可以设置powershell的透明度, 我改了一句, 想设置计算器的半透明度, 但是不能生效, 求高手指教:
第31行是原代码
第32行是自己加的
  1. function Set-ConsoleOpacity
  2. {
  3. param (
  4. [ValidateRange(10, 100)]
  5. [int]$Opacity
  6. )
  7. # Check if pinvoke type already exists, if not import the relevant functions
  8. try
  9. {
  10. $Win32Type = [Win32.WindowLayer]
  11. }
  12. catch
  13. {
  14. $Win32Type = Add-Type -MemberDefinition @'
  15.             [DllImport("user32.dll")]
  16.             public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  17.             [DllImport("user32.dll")]
  18.             public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  19.             [DllImport("user32.dll")]
  20.             public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
  21. '@ -Name WindowLayer -Namespace Win32 -PassThru
  22. }
  23. # Calculate opacity value (0-255)
  24. $OpacityValue = [int]($Opacity * 2.56) - 1
  25. # Grab the host windows handle
  26. #$ThisProcess = Get-Process -Id $PID
  27. $ThisProcess = Get-Process -name "calc"
  28. $WindowHandle = $ThisProcess.MainWindowHandle
  29. # "Constants"
  30. $GwlExStyle = -20;
  31. $WsExLayered = 0x80000;
  32. $LwaAlpha = 0x2;
  33. if ($Win32Type::GetWindowLong($WindowHandle, -20) -band $WsExLayered -ne $WsExLayered)
  34. {
  35. # If Window isn't already marked "Layered", make it so
  36. [void]$Win32Type::SetWindowLong($WindowHandle, $GwlExStyle, $Win32Type::GetWindowLong($WindowHandle, $GwlExStyle) -bxor $WsExLayered)
  37. }
  38. # Set transparency
  39. [void]$Win32Type::SetLayeredWindowAttributes($WindowHandle, 0, $OpacityValue, $LwaAlpha)
  40. }
  41. Set-ConsoleOpacity -Opacity 50
复制代码





欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2