简体   繁体   English

通过 Win + M windows 快捷方式最小化无边框表单

[英]Minimize a borderless form by Win + M windows shortcut

The standard Win + M keyboard shortcut minimizes open windows, but it does not minimize a borderless form.标准的Win + M键盘快捷键最小化打开的 windows,但不会最小化无边框形式。

I'm thinking of capturing key events of Win + M in KeyDown event but no luck, since it does not capture both but only captures the first key.我正在考虑在 KeyDown 事件中捕获Win + M的关键事件,但没有运气,因为它不会同时捕获两者,而仅捕获第一个键。

I can't programmatically minimize it using a keyboard shortcut我无法使用键盘快捷键以编程方式最小化它

Any idea on how to minimize my for form by keyboard shortcut?关于如何通过键盘快捷键最小化我的表单的任何想法? or can I capture Win + M to trigger minimize function?或者我可以捕获Win + M以触发最小化 function?

I'm using borderless form.我正在使用无边界形式。 To reproduce the problem, create a Form and set its BorderStyle to None and run the application.要重现此问题,请创建一个 Form 并将其 BorderStyle 设置为 None 并运行该应用程序。 When you press Win + M , all the windows minimize, but my borderless form stays normal.当您按Win + M时,所有 windows 最小化,但我的无边框形式保持正常。

As an option you can enable system menu (having minimize window style ) for your borderless form, then while it doesn't show the control box, but the minimize command will work as expected.作为一个选项,您可以为您的无边界表单启用系统菜单(具有最小化window 样式),然后虽然它不显示控制框,但最小化命令将按预期工作。

Add the following piece of code to your Form and then Win + M will work as expected:将以下代码添加到您的Form中,然后Win + M将按预期工作:

private const int WS_SYSMENU = 0x80000;
private const int WS_MINIMIZEBOX = 0x20000;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams p = base.CreateParams;
        p.Style = WS_SYSMENU | WS_MINIMIZEBOX;
        return p;
    }
}

As a totally different option you can register a low level keyboard hook using SetWindowsHookEx and check if you received the Win + M , then minimize the window and let the key goes through other windows.作为一个完全不同的选项,您可以使用SetWindowsHookEx注册一个低级键盘挂钩并检查您是否收到Win + M ,然后最小化 window 并让密钥通过其他 windows。

Add the following piece of code to your Form and then Win + M will work as expected:将以下代码添加到您的Form中,然后Win + M将按预期工作:

private const int WH_KEYBOARD_LL = 13;
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
    public Keys vkCode;
    public int scanCode;
    public int flags;
    public int time;
    public IntPtr dwExtraInfo;
}
private delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(
    int idHook, HookProc lpfn, IntPtr hmod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(
    IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern short GetKeyState(int keyCode);
private const int KEY_PRESSED = 0x8000;
private static bool IsKeyDown(Keys key)
{
    return Convert.ToBoolean(GetKeyState((int)key) & KEY_PRESSED);
}

private IntPtr ptrHook;
private HookProc hookProc;
private IntPtr CaptureKeys(int code, IntPtr wParam, IntPtr lParam)
{
    if (code >= 0)
    {
        KBDLLHOOKSTRUCT objKeyInfo = 
            (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(
                lParam, typeof(KBDLLHOOKSTRUCT));
        if (objKeyInfo.vkCode == (Keys.M) &&
            (IsKeyDown(Keys.LWin) || IsKeyDown(Keys.RWin)))
        {
            this.WindowState = FormWindowState.Minimized;
            return (IntPtr)0;
        }
    }
    return CallNextHookEx(ptrHook, code, wParam, lParam);
}
bool HasAltModifier(int flags)
{
    return (flags & 0x20) == 0x20;
}
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    var module = Process.GetCurrentProcess().MainModule;
    hookProc = new HookProc(CaptureKeys);
    ptrHook = SetWindowsHookEx(
        WH_KEYBOARD_LL, hookProc, GetModuleHandle(module.ModuleName), 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM