日期:2014-05-18 浏览次数:20967 次
//获取窗体大小的API
[DllImport("user32")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
//RECT结构体定义
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//屏幕大小
Rectangle rect = Screen.PrimaryScreen.Bounds;
foreach (Process p in Process.GetProcesses())
{
//找到想要的进程
if(p.ProcessName == "Maxthon")
{
RECT r;
GetWindowRect(p.MainWindowHandle,out r);
//比较
if((r.Right - r.Left) >= rect.Width && (r.Bottom - r.Top) >= rect.Height)
{
//to do,这种情况肯定已经是全屏了
}
}
}
------解决方案--------------------
要用API,c#本身办不到
首先声明一个结构,API函数要用到
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//取得前台窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
//取得桌面窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
//取得Shell窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetShellWindow();
//取得窗口大小函数
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
//桌面窗口句柄
private IntPtr desktopHandle; //Window handle for the desktop
//Shell窗口句柄
private IntPtr shellHandle; //Window handle for the shell
因为桌面窗口和Shell窗口也是全屏,要排除在其他全屏程序之外。
//取得桌面和Shell窗口句柄
desktopHandle = GetDesktopWindow();
shellHandle = GetShellWindow();
//取得前台窗口句柄并判断是否全屏
bool runningFullScreen = false;
RECT appBounds;
Rectangle screenBounds;
IntPtr hWnd;
//取得前台窗口
hWnd = GetForegroundWindow();
if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
{
//判断是否桌面或shell
if (!(hWnd.Equals(desktopHandle) ¦ ¦ hWnd.Equals(shellHandle)))
{
//取得窗口大小
GetWindowRect(hWnd, out appBounds);
//判断是否全屏
screenBounds = Screen.FromHandle(hWnd).Bounds;
if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width)
runningFullScreen = true;
}
}