就像问题所说,我能否查看其他程序是否在全屏运行?
全屏表示整个屏幕被遮盖,可能在不同的视频模式下运行而不是桌面模式。
就像问题所说,我能否查看其他程序是否在全屏运行?
全屏表示整个屏幕被遮盖,可能在不同的视频模式下运行而不是桌面模式。
这里有一段实现它的代码。你需要注意多屏幕情况,特别是在使用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);
}
screen.WorkingArea
替换 screen.Bounds
。 - Simon Mourier我进行了一些修改。使用下面的代码时,当任务栏隐藏或在第二个屏幕上时,它不会错误地返回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;
}
}