钩来检测最小化窗口C# [英] Hook to detect Minimize Window C#

查看:210
本文介绍了钩来检测最小化窗口C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有
如何在C#中检测到用户单击外部程序(例如记事本)的最小化按钮?
感谢

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

推荐答案

这应该可以工作:

    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;
    }
}

编辑:重新阅读您的问题,当记事本得到最小化时希望通知。你可以使用上面的代码在计时器轮询状态的变化。

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.

这篇关于钩来检测最小化窗口C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆