WINFORM程序如何检测当前系统中是否有正在全屏运行的程序(C#代码段分享)

做了个小工具,检测当前系统中有没有正在运行的全屏应用,自己不用写,网上也还挺不好找,最后找了一段,有效

代码是这样:

      [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(Screen screen)
        {
            if (screen == null)
            {
                screen = Screen.PrimaryScreen;
            }
            RECT rect = new RECT();
            IntPtr hWnd = (IntPtr)GetForegroundWindow();
            GetWindowRect(new HandleRef(null, hWnd), ref rect);
            if (screen.Bounds.Width == (rect.right - rect.left) && screen.Bounds.Height == (rect.bottom - rect.top))
            {
                Console.WriteLine("Fullscreen");
                return true;
            }
            else
            {
                Console.WriteLine("No Fullscreen");
                return false;
            }
        }

使用:

bool isFullScreen = IsForegroundFullScreen(null);


  • 评论列表:
  •  访客
     发布于 2022-06-25  回复该评论
  • 大佬,这段代码我如何在JS中调用,我是一个前端开发。最近在做 electron 的项目,有这样的一个需求
    •  天才小网管
       发布于 2022-06-25  回复该评论
    • JS应该调用不了这个C#代码,但 electron 本身可能有这样的检测功能,你可以查查看,不是很熟悉 electron ,帮不了你喽

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

桂ICP备19000498号-3