返回列表 发帖

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

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

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

TOP

参考一下文档里的例子
#https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-registerhotkey
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class HotKey
{
    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    [DllImport("user32.dll")]
    public static extern bool GetMessage(uint[] lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
}
"@
if ([HotKey]::RegisterHotKey(
    0,
    1,
    1 + 0x4000,
    0x42))  #0x42 is 'b'
{
    "Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"
}
$msg = new-object int[] 7
while ([HotKey]::GetMessage($msg, 0, 0, 0) -ne 0)
{
    if ($msg[2] -eq 0x0312)
    {
"WM_HOTKEY received\n"
    }
}COPY

TOP

回复 3# idwma


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

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

QQ 20147578

TOP

回复 4# czjt1234


确实理解错了
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class HotKey
{
    [DllImport("user32.dll")]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
"@
$hwnd=(ps notepad).MainWindowHandle[0]
$size=[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Size
[HotKey]::MoveWindow($hwnd, $size.width/2, 0, $size.width/2, $size.height, $true)COPY

TOP

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

TOP

返回列表