moveWindow.ps1 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # 获取 "BlueStacks App Player" 的窗口句柄
  2. $bluestacks = Get-Process | Where-Object { $_.MainWindowTitle -eq "BlueStacks App Player" } | Select-Object -First 1
  3. $hwnd = ($bluestacks.MainWindowHandle)
  4. # 定义调整窗口大小和将窗口移动到前台的函数:MoveWindowToFrontAndResize
  5. function MoveWindowToFrontAndResize {
  6. param (
  7. [Parameter(Mandatory=$true)] [IntPtr]$Handle,
  8. [Parameter(Mandatory=$true)] [int]$x,
  9. [Parameter(Mandatory=$true)] [int]$y,
  10. [Parameter(Mandatory=$true)] [int]$width,
  11. [Parameter(Mandatory=$true)] [int]$height
  12. )
  13. # 加载 user32.dll
  14. $user32 = Add-Type -Name "User32" -Namespace "Win32" -PassThru `
  15. -MemberDefinition @"
  16. [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
  17. [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
  18. "@
  19. # 将窗口移动到前台
  20. $null = $user32::SetForegroundWindow($Handle)
  21. # 延迟一段时间确保窗口已经是前台
  22. Start-Sleep -Seconds 1
  23. # 调整窗口大小和位置
  24. $null = $user32::MoveWindow($Handle, $x, $y, $width, $height, $true)
  25. }
  26. # 定义窗口的新位置和大小
  27. $x = 100
  28. $y = 100
  29. $width = 1170
  30. $height = 563
  31. # 如果找到了窗口句柄,则移动窗口到前台并调整其大小
  32. if ($hwnd -ne 0) {
  33. MoveWindowToFrontAndResize -Handle $hwnd -x $x -y $y -width $width -height $height
  34. Write-Host "窗口已移动到前台并调整为宽度: $width, 高度: $height."
  35. } else {
  36. Write-Host "未能找到 'BlueStacks App Player' 窗口。"
  37. }