2011-03-15 51 views
2

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

+0

我很确定你不能。 – 2011-03-15 21:08:08

+0

这需要使用SetWindowsHookEx()向进程中注入DLL。您不能在托管代码中编写这样的DLL。 – 2011-03-15 21:12:50

回答

0

汉斯帕赞特说,你不能得到它最小化的事件。

虽然,我相信你可以存储窗口的状态,看看它们是否在稍后的时间间隔最小化。 通过使用GetWindowPlacement Function

2

这应该工作:

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

编辑:刚才重读你的问题,并注意到你想通知当记事本获得最小化。那么你可以在定时器中使用上面的代码来查询状态变化。

相关问题