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

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

[复制链接]
发表于 2024-2-14 17:50:20 | 显示全部楼层 |阅读模式
我想用Powershell代码将notepad记事本窗口实现win+右方向键的窗口效果, 就是把窗口调到屏幕的一半并右对齐
发表于 2024-2-15 21:01:53 | 显示全部楼层
只知道可以调win api ,很麻烦
发表于 2024-2-17 15:58:13 | 显示全部楼层
参考一下文档里的例子
  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. }
复制代码
发表于 2024-2-17 20:52:14 | 显示全部楼层
回复 3# idwma


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

估摸着楼主是想ps一直运行着,检测到记事本窗口就对其发送快捷键
发表于 2024-2-17 22:12:37 | 显示全部楼层
回复 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)
复制代码
发表于 2024-2-18 05:29:44 | 显示全部楼层
把3楼跟5楼的代码合起来就差不多了
成功GetMessage后,对记事本MoveWindow
想要恢复的还得记住之前的位置
完事后可能还要UnRegisterHotKey
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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