找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 10869|回复: 0

[问题求助] PowerShell怎样根据程序名设置窗口半透明效果

[复制链接]
发表于 2021-12-13 19:19:27 | 显示全部楼层 |阅读模式
找了一段代码, 可以设置powershell的透明度, 我改了一句, 想设置计算器的半透明度, 但是不能生效, 求高手指教:
第31行是原代码
第32行是自己加的
  1. function Set-ConsoleOpacity
  2. {
  3.         param (
  4.                 [ValidateRange(10, 100)]
  5.                 [int]$Opacity
  6.         )
  7.        
  8.         # Check if pinvoke type already exists, if not import the relevant functions
  9.         try
  10.         {
  11.                 $Win32Type = [Win32.WindowLayer]
  12.         }
  13.         catch
  14.         {
  15.                 $Win32Type = Add-Type -MemberDefinition @'
  16.             [DllImport("user32.dll")]
  17.             public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  18.             [DllImport("user32.dll")]
  19.             public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

  20.             [DllImport("user32.dll")]
  21.             public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
  22. '@ -Name WindowLayer -Namespace Win32 -PassThru
  23.         }
  24.        
  25.         # Calculate opacity value (0-255)
  26.         $OpacityValue = [int]($Opacity * 2.56) - 1
  27.        
  28.         # Grab the host windows handle
  29.         #$ThisProcess = Get-Process -Id $PID
  30.         $ThisProcess = Get-Process -name "calc"
  31.         $WindowHandle = $ThisProcess.MainWindowHandle
  32.        
  33.         # "Constants"
  34.         $GwlExStyle = -20;
  35.         $WsExLayered = 0x80000;
  36.         $LwaAlpha = 0x2;
  37.        
  38.         if ($Win32Type::GetWindowLong($WindowHandle, -20) -band $WsExLayered -ne $WsExLayered)
  39.         {
  40.                 # If Window isn't already marked "Layered", make it so
  41.                 [void]$Win32Type::SetWindowLong($WindowHandle, $GwlExStyle, $Win32Type::GetWindowLong($WindowHandle, $GwlExStyle) -bxor $WsExLayered)
  42.         }
  43.        
  44.         # Set transparency
  45.         [void]$Win32Type::SetLayeredWindowAttributes($WindowHandle, 0, $OpacityValue, $LwaAlpha)
  46. }

  47. Set-ConsoleOpacity -Opacity 50
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 02:46 , Processed in 0.017192 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表