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

[问题求助] 用Powershell控制CMD窗口的位置和大小

[复制链接]
 楼主| 发表于 2022-2-17 21:18:18 | 显示全部楼层
回复 15# went


    感谢, 太牛X了, 看不懂了, 先收藏了
 楼主| 发表于 2022-2-18 12:09:44 | 显示全部楼层
本帖最后由 5i365 于 2022-2-18 12:12 编辑

回复 15# went
使用了@idwma 大侠这个贴子的代码, 取得屏幕宽度, 现在代码少了些
http://www.bathome.net/redirect. ... 1487&pid=251249

  1. #Windows API
  2. $code=@'
  3.     using System.Runtime.InteropServices;
  4.     public struct RECT{
  5.         public uint left;
  6.         public uint top;
  7.         public uint right;
  8.         public uint bottom;
  9.     }
  10.     public static class WinApi{
  11.         [DllImport("user32.dll")]
  12.         public static extern bool SetWindowPos(uint hWnd,uint hAfter,uint x,uint y,uint cx,uint cy,uint flags);
  13.         [DllImport("user32.dll")]
  14.         public static extern bool GetWindowRect(uint hwnd, ref RECT rect);
  15.     }
  16. '@
  17. Add-Type -TypeDefinition $code

  18. #获取记事本窗口句柄
  19. $hwnd = (Get-Process 'notepad')[0].MainWindowHandle
  20. #获取窗口信息
  21. $rect = New-Object 'RECT'
  22. [void][WinApi]::GetWindowRect([int]$hwnd,[ref]$rect)
  23. $screen_w = [Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width
  24. #计算水平居中坐标
  25. $x = ($screen_w - ($rect.right - $rect.left))/2
  26. #设置记事本水平居中
  27. [WinApi]::SetWindowPos([int]$hwnd,$null,$x,$rect.top,0,0,1)
复制代码
发表于 2023-3-4 11:45:11 | 显示全部楼层
回复 15# went


    我尝试用这段代码来调整edge和chrome发现不行 是因为进程太多的原因吗?获取的句柄为0
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-17 03:52 , Processed in 0.013779 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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