2015-08-27 53 views
2

我想檢查一下,如果任何屏幕主機應用程序全屏模式。我的解決方案只有一個屏幕,該屏幕是代碼從這裏複製:[WPF] [C#] How-to : Detect if another application is running in full screen mode.該解決方案是基於如何檢查應用程序是否在任何屏幕上運行全屏模式?

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

其中僅收集活躍窗口句柄。問題是,我有兩個屏幕。我搜索了很多網站,但沒有人回答我的問題。這不是捕捉屏幕截圖,這很簡單,不依賴於P/Invoke。

這可能嗎?

+0

'GetForgroundWindow'只是給你一個窗口句柄(hWnd)。使用任何你喜歡的枚舉窗口的方法 - 它們都會給你一個hWnd - 並且插入這些值而不是前景hWnd。 –

+0

試試這個:http://www.codewrecks.com/blog/index.php/2014/01/29/change-appearance-of-a-control-if-the-windows-is-maximized-in-wpf/通過使用綁定,你可以存儲變量來檢查sceen是否最大化 – PieterSchool

+0

@PieterSchool - 這不是關於WPF,但感謝您的時間:) – Fka

回答

1

不準備使用的解決方案在這裏,但讓我們來看看..顯示

所有的獲取列表窗口,並檢查這些窗口的位置和大小 - 可能的話,大量的工具這樣做,對很多文章,我會跳過這一個。然後,您可以爲每個或某些窗口調用MonitorFromWindow,並根據監視器信息比較窗口維度&的位置。如果windowpos〜= 0,0和windowsize〜= monitorresolution,則可以假定此窗口處於全屏模式。另一方面,如果已經擁有所有HWND的列表,那麼爲什麼不只是Query the window for its placement並且檢查了SW_MAXIMIZE/SW_SHOWMAXIMIZED標誌的WINDOWPLACEMENT.showCmd。這不會告訴你哪個顯示器,但至少應該告訴你,如果窗口是最大化,如果它足夠你..

我不知道多快/慢它會這樣做但是,是的,這似乎是可能的。

1

您可以使用EnumWindowsScreen.FromHandle一起使用。也許GetWindowRect()進行計算。

類似的信息(僞代碼!):

//------------------------------ 
//this sample code is taken from http://pinvoke.net/default.aspx/user32/EnumWindows.html 

public delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool EnumWindows(EnumedWindow lpEnumFunc, ArrayList lParam); 

public static ArrayList GetWindows() 
{  
    ArrayList windowHandles = new ArrayList(); 
    EnumedWindow callBackPtr = GetWindowHandle; 
    EnumWindows(callBackPtr, windowHandles); 
    return windowHandles;  
} 

private static bool GetWindowHandle(IntPtr windowHandle, ArrayList windowHandles) 
{ 
    windowHandles.Add(windowHandle); 
    return true; 
} 

//------------------------------ 

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

[StructLayout(LayoutKind.Sequential)] 
private struct Rect 
{ 
    public int Left; 
    public int Top; 
    public int Right; 
    public int Bottom; 
} 

static void Main() { 
    foreach(IntPtr handle in GetWindows()) 
    { 
     Screen scr = Screen.FromHandle(handle); 

     if(IsFullscreen(handle, scr)) 
     { 
      // the window is fullscreen... 
     } 
    } 
} 

private bool IsFullscreen(IntPtr wndHandle, Screen screen) 
{ 
    Rect r = new Rect(); 
    GetWindowRect(wndHandle, ref r); 
    return new Rectangle(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top) 
          .Contains(screen.Bounds); 
} 
1

我寫的一段代碼這是工作

namespace EnumWnd 
{ 
using System; 
using System.Runtime.InteropServices; 
using System.Text; 

[StructLayout(LayoutKind.Sequential)] 
public struct Rect 
{ 
    public int Left; 

    public int Top; 

    public int Right; 

    public int Bottom; 
} 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 
internal struct MonitorInfoEx 
{ 
    public int cbSize; 
    public Rect rcMonitor; 
    public Rect rcWork; 
    public UInt32 dwFlags; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDeviceName; 
} 

internal class Program 
{ 
    [DllImport("user32.dll")] 
    public static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect); 

    [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
    protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); 

    [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
    protected static extern int GetWindowTextLength(IntPtr hWnd); 

    [DllImport("user32.dll")] 
    protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); 

    [DllImport("user32.dll")] 
    protected static extern bool IsWindowVisible(IntPtr hWnd); 

    [DllImport("User32")] 
    public static extern IntPtr MonitorFromWindow(IntPtr hWnd, int dwFlags); 

    [DllImport("user32", EntryPoint = "GetMonitorInfo", CharSet = CharSet.Auto, 
     SetLastError = true)] 
    internal static extern bool GetMonitorInfoEx(IntPtr hMonitor, ref MonitorInfoEx lpmi); 

    protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam) 
    { 
     const int MONITOR_DEFAULTTOPRIMARY = 1; 
     var mi = new MonitorInfoEx(); 
     mi.cbSize = Marshal.SizeOf(mi); 
     GetMonitorInfoEx(MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY), ref mi); 

     Rect appBounds; 
     GetWindowRect(hWnd, out appBounds); 
     int size = GetWindowTextLength(hWnd); 
     if (size++ > 0 && IsWindowVisible(hWnd)) 
     { 
      var sb = new StringBuilder(size); 
      GetWindowText(hWnd, sb, size); 

      if (sb.Length > 20) 
      { 
       sb.Remove(20, sb.Length - 20); 
      } 

      int windowHeight = appBounds.Right - appBounds.Left; 
      int windowWidth = appBounds.Bottom - appBounds.Top; 

      int monitorHeight = mi.rcMonitor.Right - mi.rcMonitor.Left; 
      int monitorWidth = mi.rcMonitor.Bottom - mi.rcMonitor.Top; 

      bool fullScreen = (windowHeight == monitorHeight) && (windowWidth == monitorWidth); 

      sb.AppendFormat(" Wnd:({0} | {1}) Mtr:({2} | {3} | Name: {4}) - {5}", windowWidth, windowHeight, monitorWidth, monitorHeight, mi.szDeviceName, fullScreen); 

      Console.WriteLine(sb.ToString()); 
     } 
     return true; 
    } 

    private static void Main() 
    { 
     while (true) 
     { 
      EnumWindows(EnumTheWindows, IntPtr.Zero); 
      Console.ReadKey(); 
      Console.Clear(); 
     } 
    } 

    protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); 
} 

}

感謝@SamAxe和@quetzalcoatl爲我提供有用的提示。

相關問題