用于检测最小化窗口的钩子 C#

发布于 2024-10-22 06:47:47 字数 45 浏览 12 评论 0原文

大家好 在C#中如何检测用户单击了外部程序(例如记事本)的最小化按钮? 谢谢

Hi all
How can I detect in C # that the user clicked on the minimize button of an external program (eg notepad)?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-10-29 06:47:47

这应该有效:

    public class myClass
    {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

    const UInt32 SW_HIDE =         0;
    const UInt32 SW_SHOWNORMAL =       1;
    const UInt32 SW_NORMAL =       1;
    const UInt32 SW_SHOWMINIMIZED =    2;
    const UInt32 SW_SHOWMAXIMIZED =    3;
    const UInt32 SW_MAXIMIZE =     3;
    const UInt32 SW_SHOWNOACTIVATE =   4;
    const UInt32 SW_SHOW =         5;
    const UInt32 SW_MINIMIZE =     6;
    const UInt32 SW_SHOWMINNOACTIVE =  7;
    const UInt32 SW_SHOWNA =       8;
    const UInt32 SW_RESTORE =      9;

    public myClass()
    {
        var proc = Process.GetProcessesByName("notepad");
        if (proc.Length > 0)
        {
            bool isNotepadMinimized = myClass.GetMinimized(proc[0].MainWindowHandle);

            if (isNotepadMinimized)
                Console.WriteLine("Notepad is Minimized!");
        }
    }

    private struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }

    public static bool GetMinimized(IntPtr handle)
    {
        WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
        placement.length = Marshal.SizeOf(placement);
        GetWindowPlacement(handle, ref placement);
        return placement.flags == SW_SHOWMINIMIZED;
    }
}

编辑:只需重新阅读您的问题,并注意到您希望在记事本最小化时收到通知。那么您可以在计时器中使用上面的代码来轮询状态更改。

This should work:

    public class myClass
    {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

    const UInt32 SW_HIDE =         0;
    const UInt32 SW_SHOWNORMAL =       1;
    const UInt32 SW_NORMAL =       1;
    const UInt32 SW_SHOWMINIMIZED =    2;
    const UInt32 SW_SHOWMAXIMIZED =    3;
    const UInt32 SW_MAXIMIZE =     3;
    const UInt32 SW_SHOWNOACTIVATE =   4;
    const UInt32 SW_SHOW =         5;
    const UInt32 SW_MINIMIZE =     6;
    const UInt32 SW_SHOWMINNOACTIVE =  7;
    const UInt32 SW_SHOWNA =       8;
    const UInt32 SW_RESTORE =      9;

    public myClass()
    {
        var proc = Process.GetProcessesByName("notepad");
        if (proc.Length > 0)
        {
            bool isNotepadMinimized = myClass.GetMinimized(proc[0].MainWindowHandle);

            if (isNotepadMinimized)
                Console.WriteLine("Notepad is Minimized!");
        }
    }

    private struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public int showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }

    public static bool GetMinimized(IntPtr handle)
    {
        WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
        placement.length = Marshal.SizeOf(placement);
        GetWindowPlacement(handle, ref placement);
        return placement.flags == SW_SHOWMINIMIZED;
    }
}

Edit: Just re-read you question and noticed you wanted to be notified when Notepad get minimized. Well you could use the code above in a timer to poll the status change.

遮了一弯 2024-10-29 06:47:47

正如汉斯·帕桑特(Hans Passant)所说,你无法将其最小化。

不过,我相信您可以存储窗口的状态,并在稍后查看它们是否最小化。
通过使用 GetWindowPlacement 函数

As Hans Passant said you cannot get the event of it being minimized.

Although, I believe you can store the states of windows and see if they are minimized at a later interval.
by using the GetWindowPlacement Function.

ペ泪落弦音 2024-10-29 06:47:47

上面的答案有一个错误。

您需要检查 placement.showCMD,而不是 placement.flags

WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(_hwnd, ref placement);
return placement.showCmd == SW_SHOWMINIMIZED;

请参阅https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85)。 ASPX

The answer above has an error.

You need to check placement.showCMD, not placement.flags

WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(_hwnd, ref placement);
return placement.showCmd == SW_SHOWMINIMIZED;

Please see https://msdn.microsoft.com/en-us/library/windows/desktop/ms632611(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文