日期:2014-05-18  浏览次数:20790 次

C# WinForm : 判 断 当 前 是 否 有 全 屏 程 序 (如 魔 兽 争 霸) 运 行
C# WinForm:
  程序中需要一个函数来判断当前是否有全屏程序运行,不是指我的C#程序的窗口是不是全屏,而是判断外界的程序
  如我的C#程序开在那 如果你要打开全屏游戏比如魔兽争霸之类的的时候 要在我的C#程序里得知 然后将this.Text="有全屏程序运行";
  请问是否有相关的帮助资料或直接可用的API函数等 如果涉及系统钩子的话 希望可以给点示例代码 谢谢

------解决方案--------------------
C# code

        //获取窗体大小的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函数要用到 
C# code


        [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; 
                } 
            }