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

解决方案 »

  1.   


            //获取窗体大小的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,这种情况肯定已经是全屏了
                           }
                    }
                }
      

  2.   

    要用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; 
                    } 
                } 
      

  3.   

    倒是有几个API函数.getwindowrect....名字记不全了...差不多就是枚举所有窗口.获取尺寸是否等于当前屏幕大小..但是这个方法也不怎么科学 ..能有检查屏幕是否被独占的方法更好..
      

  4.   

    为什么我用了6楼的说//取得桌面和Shell窗口句柄 
    desktopHandle = GetDesktopWindow();方法必须返回值!!!我已经添加了 using System.Runtime.InteropServices; 
      

  5.   

    自己从别处找到了代码!http://www.csharpwin.com/csharpspace/10543r9203.shtml注册一个AppBar(什么是AppBar?Using Application Desktop Toolbars),通过SHAppBarMessage向系统注册AppBar,这样,当有程序全屏运行时系统会向我们的程序发送消息,在窗体WndProc中处理即可。声明要使用到的API和常量:    public class APIWrapper
        {
            [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
            public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);        [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int RegisterWindowMessage(string msg);    }    [StructLayout(LayoutKind.Sequential)]
        public struct APPBARDATA
        {
            public int cbSize;
            public IntPtr hWnd;
            public int uCallbackMessage;
            public int uEdge;
            public RECT rc;
            public IntPtr lParam;
        }    public enum ABMsg : int
        {
            ABM_NEW = 0,
            ABM_REMOVE,
            ABM_QUERYPOS,
            ABM_SETPOS,
            ABM_GETSTATE,
            ABM_GETTASKBARPOS,
            ABM_ACTIVATE,
            ABM_GETAUTOHIDEBAR,
            ABM_SETAUTOHIDEBAR,
            ABM_WINDOWPOSCHANGED,
            ABM_SETSTATE
        }    public enum ABNotify : int
        {
            ABN_STATECHANGE = 0,
            ABN_POSCHANGED,
            ABN_FULLSCREENAPP,
            ABN_WINDOWARRANGE
        }    public enum ABEdge : int
        {
            ABE_LEFT = 0,
            ABE_TOP,
            ABE_RIGHT,
            ABE_BOTTOM
        }[StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;    public override string ToString()
        {
            return "{left=" + left.ToString() + ", " + "top=" + top.ToString() + ", " +
                "right=" + right.ToString() + ", " + "bottom=" + bottom.ToString() + "}";
            }
    }重载窗口消息处理函数: 
            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (m.Msg == uCallBackMsg)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case (int)ABNotify.ABN_FULLSCREENAPP:
                            {
                                if ((int)m.LParam == 1)
                                    this.RunningFullScreenApp = true;
                                else
                                    this.RunningFullScreenApp = false;
                                break;
                            }
                        default:
                            break;
                    }
                }            base.WndProc(ref m);
            }在程序退出时,不要忘了Unregister我们注册的AppBar: 
    this.RegisterAppBar(true);以上介绍的是C# Winform程序判断是否有全屏窗口程序正在运行,希望队你有所帮助。