2010-09-18 87 views
4

就像問題所述。我能否看到其他人,程序是否正在全屏運行?有沒有辦法檢查是否有其他程序全屏運行

全屏意味着整個屏幕被遮擋,可能在與桌面不同的視頻模式下運行。

+0

你想知道你的程序控制程序是否被完全屏蔽,或者你想知道另一個程序是否正在全屏運行嗎? – 2010-09-19 00:05:54

+2

相關:http://stackoverflow.com/questions/3536373/detect-if-user-has-any-application-running-in-fullscreen。另外http://stackoverflow.com/questions/3686311/my-c-winform-needs-to-detect-when-other-applications-enter-exit-run-in-tr-full。我懷疑後者會回答你的問題。 – 2010-09-19 01:56:05

回答

7

這是一些代碼。你想要關心多屏幕的情況下,特別是像Powerpoint的應用程序

[StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
     public int left; 
     public int top; 
     public int right; 
     public int bottom; 
    } 

    [DllImport("user32.dll")] 
    private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect); 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetForegroundWindow(); 

    public static bool IsForegroundFullScreen() 
    { 
     return IsForegroundFullScreen(null); 
    } 

    public static bool IsForegroundFullScreen(Screen screen) 
    { 
     if (screen == null) 
     { 
      screen = Screen.PrimaryScreen; 
     } 
     RECT rect = new RECT(); 
     GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect); 
     return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top).Contains(screen.Bounds); 
    } 
+1

如果你隱藏任務欄,這將錯誤地報告全屏應用 – Rolle 2014-02-04 14:46:18

+0

@Rolle是正確的。任何解決方案?我想很難將全屏視頻/遊戲/ PowerPoint演示文稿中運行輔助屏幕(無任務欄)的最大化記事本分開,而不考慮每個過程。 – 2015-06-18 06:40:40

+0

不知道究竟你以後,但如果你想在帳戶任務欄,你可以通過替換''screen.WorkingArea' screen.Bounds'。 – 2015-06-18 08:15:07

0

我做了一些修改。使用下面的代碼,當任務欄被隱藏或在第二個屏幕上時,它不會錯誤地返回true。在Win 7下測試。

[StructLayout(LayoutKind.Sequential)] 
    public struct RECT 
    { 
     public int left; 
     public int top; 
     public int right; 
     public int bottom; 
    } 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

    [DllImport("user32.dll")] 
    private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect); 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetForegroundWindow(); 

    public static bool IsForegroundFullScreen() 
    { 
     return IsForegroundFullScreen(null); 
    } 


    public static bool IsForegroundFullScreen(System.Windows.Forms.Screen screen) 
    { 

     if (screen == null) 
     { 
      screen = System.Windows.Forms.Screen.PrimaryScreen; 
     } 
     RECT rect = new RECT(); 
     IntPtr hWnd = (IntPtr)GetForegroundWindow(); 


     GetWindowRect(new HandleRef(null, hWnd), ref rect); 

     /* in case you want the process name: 
     uint procId = 0; 
     GetWindowThreadProcessId(hWnd, out procId); 
     var proc = System.Diagnostics.Process.GetProcessById((int)procId); 
     Console.WriteLine(proc.ProcessName); 
     */ 


     if (screen.Bounds.Width == (rect.right - rect.left) && screen.Bounds.Height == (rect.bottom - rect.top)) 
     { 
      Console.WriteLine("Fullscreen!") 
      return true; 
     } 
     else { 
      Console.WriteLine("Nope, :-("); 
      return false; 
     } 


    } 
相關問題