2011-03-15 50 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; 
    } 
} 

編輯:剛纔重讀你的問題,並注意到你想通知當記事本獲得最小化。那麼你可以在定時器中使用上面的代碼來查詢狀態變化。

相關問題