且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

钩来检测最小化窗口C#

更新时间:2023-02-06 15:56:32

这应该可以工作:

    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.