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

[问题求助] 怎样用Powershell代码实现win+右方向键的窗口效果

我想用Powershell代码将notepad记事本窗口实现win+右方向键的窗口效果, 就是把窗口调到屏幕的一半并右对齐

只知道可以调win api ,很麻烦

TOP

参考一下文档里的例子
  1. #https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-registerhotkey
  2. Add-Type @"
  3. using System;
  4. using System.Runtime.InteropServices;
  5. public class HotKey
  6. {
  7.     [DllImport("user32.dll")]
  8.     public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
  9.     [DllImport("user32.dll")]
  10.     public static extern bool GetMessage(uint[] lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
  11. }
  12. "@
  13. if ([HotKey]::RegisterHotKey(
  14.     0,
  15.     1,
  16.     1 + 0x4000,
  17.     0x42))  #0x42 is 'b'
  18. {
  19.     "Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"
  20. }
  21. $msg = new-object int[] 7
  22. while ([HotKey]::GetMessage($msg, 0, 0, 0) -ne 0)
  23. {
  24.     if ($msg[2] -eq 0x0312)
  25.     {
  26. "WM_HOTKEY received\n"
  27.     }
  28. }
复制代码

TOP

回复 3# idwma


这个好像是检测热键,不是发送

估摸着楼主是想ps一直运行着,检测到记事本窗口就对其发送快捷键

QQ 20147578

TOP

回复 4# czjt1234


确实理解错了
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type @"
  3. using System;
  4. using System.Runtime.InteropServices;
  5. public class HotKey
  6. {
  7.     [DllImport("user32.dll")]
  8.     public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  9. }
  10. "@
  11. $hwnd=(ps notepad).MainWindowHandle[0]
  12. $size=[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Size
  13. [HotKey]::MoveWindow($hwnd, $size.width/2, 0, $size.width/2, $size.height, $true)
复制代码

TOP

把3楼跟5楼的代码合起来就差不多了
成功GetMessage后,对记事本MoveWindow
想要恢复的还得记住之前的位置
完事后可能还要UnRegisterHotKey

TOP

返回列表