[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
直接用winapi就行,别搞那么复杂
  1. #@&cls&powershell "type '%~0'|out-string|iex"&pause&exit
  2. #Windows API
  3. $code=@'
  4.     using System.Runtime.InteropServices;
  5.     public static class WinApi{
  6.         [DllImport("user32.dll")]
  7.         public static extern bool SetWindowPos(uint hWnd,uint hAfter,uint x,uint y,uint cx,uint cy,uint flags);
  8.         [DllImport("kernel32.dll")]
  9.         public static extern uint GetConsoleWindow();
  10.     }
  11. '@
  12. Add-Type -TypeDefinition $code
  13. #设置位置及大小
  14. $hwnd = [WinApi]::GetConsoleWindow()
  15. [void][WinApi]::SetWindowPos($hwnd,$null,30,30,400,500,0)
  16. "hello,  world"
  17. cmd /c pause
复制代码
1

评分人数

    • 5i365: 乐于分享, 技术牛X技术 + 1

TOP

回复 4# 5i365
  1. $p = Get-Process -Name 'notepad'
  2. [void][WinApi]::SetWindowPos([int]$p.MainWindowHandle,$null,30,30,400,500,0)
复制代码

TOP

最大化最小化
  1. [DllImport("user32.dll")]
  2. public static extern bool ShowWindow(uint hWnd,uint show);
复制代码
  1. $SW_NORMAL = 1
  2. $SW_MAXIMIZE = 3
  3. $SW_MINIMIZE = 6
  4. $p = Get-Process -Name 'notepad'
  5. [WinApi]::ShowWindow([int]$p.MainWindowHandle,$SW_MINIMIZE)
复制代码

TOP

IntPtr实质就是一个uint,你去看看win32编程就不会有这么多疑问了
  1. [DllImport("user32.dll")]
  2. public static extern bool ShowWindowAsync(uint hWnd, int nCmdShow);
复制代码
  1. # 最小化窗口
  2. [void][WinApi]::ShowWindowAsync([int]$hwnd, 2)
  3. # 恢复窗口
  4. [void][WinApi]::ShowWindowAsync([int]$hwnd, 4)
复制代码

TOP

powershell调用api这么麻烦,不如去用c++

TOP

  1. cls
  2. #Windows API
  3. $code = @'
  4.     using System.Runtime.InteropServices;
  5.     public static class WinApi{
  6.         [DllImport("user32.dll")]
  7.         public static extern bool SetWindowPos(uint hWnd,uint hAfter,uint x,uint y,uint cx,uint cy,uint flags);
  8.         [DllImport("kernel32.dll")]
  9.         public static extern uint GetConsoleWindow();
  10. [DllImport("user32.dll")]
  11. public static extern bool ShowWindowAsync(uint hWnd, int nCmdShow);
  12.     }
  13. '@
  14. Add-Type -TypeDefinition $code
  15. #设置CMD窗口位置及大小
  16. $hwnd = [WinApi]::GetConsoleWindow()
  17. [void][WinApi]::SetWindowPos($hwnd, $null, 30, 30, 400, 500, 0)
  18. #-------------------------------------------------------------------------
  19. #启动记事本
  20. Stop-Process -Name 'notepad' -Force -ErrorAction SilentlyContinue
  21. Start-Process 'notepad'
  22. Start-Sleep -Seconds 1
  23. $hwnd = (Get-Process 'notepad')[0].MainWindowHandle
  24. # 最小化窗口
  25. [void][WinApi]::ShowWindowAsync([int]$hwnd, 2)
  26. # 恢复窗口
  27. [void][WinApi]::ShowWindowAsync([int]$hwnd, 4)
  28. # 关闭记事本
  29. #Stop-Process -Name Notepad
复制代码

TOP

窗口居中比较麻烦,记事本窗口居中代码如下,cmd窗口同理
  1. #&cls&@powershell -c "Get-Content '%~0' | Out-String | Invoke-Expression" &pause&exit
  2. cls
  3. #Windows API
  4. $code=@'
  5.     using System;
  6.     using System.Runtime.InteropServices;
  7.     public struct RECT{
  8.         public uint left;
  9.         public uint top;
  10.         public uint right;
  11.         public uint bottom;
  12.     }
  13.     public static class WinApi{
  14.         [DllImport("user32.dll")]
  15.         public static extern bool SetWindowPos(uint hWnd,uint hAfter,uint x,uint y,uint cx,uint cy,uint flags);
  16.         [DllImport("kernel32.dll")]
  17.         public static extern uint GetConsoleWindow();
  18.         [DllImport("user32.dll")]
  19.         public static extern bool GetWindowRect(uint hwnd, ref RECT rect);
  20.         [DllImport("user32.dll")]
  21.         public static extern uint GetDC(uint hwnd);
  22.         [DllImport("gdi32.dll")]
  23.         public static extern uint GetDeviceCaps(uint hdc, int index);
  24.         public static uint[] GetScreen(){
  25.             uint[] arr = {0,0};
  26.             uint hdc = GetDC(0);
  27.             arr[0] = GetDeviceCaps(hdc,118);
  28.             arr[1] = GetDeviceCaps(hdc,117);
  29.             return arr;
  30.         }
  31.     }
  32. '@
  33. Add-Type -TypeDefinition $code
  34. #获取记事本窗口句柄
  35. $hwnd = (Get-Process 'notepad')[0].MainWindowHandle
  36. #获取窗口信息
  37. $rect = New-Object 'RECT'
  38. [void][WinApi]::GetWindowRect([int]$hwnd,[ref]$rect)
  39. $screen = [WinApi]::GetScreen()
  40. #计算水平居中坐标
  41. $x = ($screen[0] - ($rect.right - $rect.left))/2
  42. #设置记事本水平居中
  43. [WinApi]::SetWindowPos([int]$hwnd,$null,$x,$rect.top,0,0,1)
复制代码
1

评分人数

    • 5i365: 技术牛X, 乐于助人技术 + 1

TOP

返回列表