检查是否有任何系统应用程序正在全屏运行nodeJS

问题描述 投票:0回答:2

我正在电子中制作这个小部件,当您按 ctrl+space 时,它会显示在屏幕上。 我只希望当没有应用程序在全屏模式下运行时小部件能够显示。

我尝试过使用 powershell 但似乎不起作用

const { exec } = require('child_process')
function isFullscreenAppRunning() {
    return new Promise((resolve, reject) => {
        const script = `
        function Is-AppFullScreen {
                param(
                [Parameter(Mandatory = $true)]
                [string] $ProcessName
                )
            
                # Get main screen resolution
                $screenWidth = (Get-Screen).WorkingArea.Width
                $screenHeight = (Get-Screen).WorkingArea.Height
            
                # Get window of the process
                $window = Get-Process -Name $ProcessName | Where-Object {$_.MainWindowTitle -ne $null} | Select-Object -ExpandProperty MainWindowHandle
            
                if ($window) {
                # Get window dimensions (accounting for borders)
                $windowRect = [System.Drawing.Rectangle]::FromLTRB((Get-Window -Handle $window).Left, (Get-Window -Handle $window).Top, (Get-Window -Handle $window).Right, (Get-Window -Handle $window).Bottom)
                $windowWidth = $windowRect.Width - (Get-Window -Handle $window).BorderWidth
                $windowHeight = $windowRect.Height - (Get-Window -Handle $window).BorderHeight
            
                # Check if window dimensions match screen resolution (considering potential rounding errors)
                return ($windowWidth -eq $screenWidth -and $windowHeight -eq $screenHeight)
                } else {
                # Process not found or no main window
                return $false
                }
            }
            
            # Check all processes with a main window title
            $isAnyAppFullScreen = $false
            Get-Process | Where-Object {$_.MainWindowTitle -ne $null} | ForEach-Object {
                $processName = $_.Name
                if (Is-AppFullScreen -ProcessName $processName) {
                $isAnyAppFullScreen = $true
                # Exit loop once a fullscreen app is found (optional for performance)
                break
                }
            }
            
            # Return true if any app is fullscreen, false otherwise
            return $isAnyAppFullScreen
            `
        
        exec(`powershell -command "${script}"`, (err, stdout, stderr) => {
            if (err) {
            reject(err)
            return
            }
            resolve(stdout.trim() === 'True')
        })
    })
}
javascript node.js electron
2个回答
0
投票

您也可以尝试这些代码

Add-Type @"
    using System;
    using System.Runtime.InteropServices;

    public class Win32 {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }
"@

function IsAppFullScreen {
    param (
        [Parameter(Mandatory = $true)]
        [string] $ProcessName
    )

    $processes = Get-Process | Where-Object { $_.MainWindowTitle -ne "" }

    foreach ($process in $processes) {
        $handle = $process.MainWindowHandle
        $rect = New-Object Win32.RECT

        if (-not [Win32]::GetWindowRect($handle, [ref]$rect)) {
            continue
        }

        $width = $rect.Right - $rect.Left
        $height = $rect.Bottom - $rect.Top

        $screenWidth = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width
        $screenHeight = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height

        if ($width -eq $screenWidth -and $height -eq $screenHeight) {
            return $true
        }
    }

    return $false
}

# Check if any app is fullscreen
$isAnyAppFullScreen = $false
$isAnyAppFullScreen = IsAppFullScreen

# Return true if any app is fullscreen, false otherwise
$isAnyAppFullScreen

和 NodeJs

const { exec } = require('child_process');

function isFullscreenAppRunning() {
    return new Promise((resolve, reject) => {
        const script = `
            function Is-AppFullScreen {
                param(
                    [Parameter(Mandatory = $true)]
                    [string] $ProcessName
                )
                
                # Get main screen resolution
                $screenWidth = (Get-WmiObject Win32_DesktopMonitor).ScreenWidth
                $screenHeight = (Get-WmiObject Win32_DesktopMonitor).ScreenHeight
                
                # Get window of the process
                $window = Get-Process -Name $ProcessName | Where-Object {$_.MainWindowTitle -ne $null} | Select-Object -ExpandProperty MainWindowHandle
                
                if ($window) {
                    # Get window dimensions (accounting for borders)
                    $windowRect = [System.Drawing.Rectangle]::FromLTRB((Get-Window -Handle $window).Left, (Get-Window -Handle $window).Top, (Get-Window -Handle $window).Right, (Get-Window -Handle $window).Bottom)
                    $windowWidth = $windowRect.Width - (Get-Window -Handle $window).BorderWidth
                    $windowHeight = $windowRect.Height - (Get-Window -Handle $window).BorderHeight
                    
                    # Check if window dimensions match screen resolution (considering potential rounding errors)
                    return ($windowWidth -eq $screenWidth -and $windowHeight -eq $screenHeight)
                } else {
                    # Process not found or no main window
                    return $false
                }
            }
            
            # Check all processes with a main window title
            $isAnyAppFullScreen = $false
            Get-Process | Where-Object {$_.MainWindowTitle -ne $null} | ForEach-Object {
                $processName = $_.Name
                if (Is-AppFullScreen -ProcessName $processName) {
                    $isAnyAppFullScreen = $true
                    # Exit loop once a fullscreen app is found (optional for performance)
                    break
                }
            }
            
            # Return true if any app is fullscreen, false otherwise
            return $isAnyAppFullScreen
        `;
        
        exec(`powershell -command "${script}"`, (err, stdout, stderr) => {
            if (err) {
                reject(err);
                return;
            }
            resolve(stdout.trim() === 'True');
        });
    });
}

// Usage
isFullscreenAppRunning().then(result => {
    console.log('Is any app running in fullscreen?', result);
}).catch(error => {
    console.error('Error:', error);
});

-1
投票

这是一个 JS 工作,而不是 Node.js...

// Detect when a frame/window is in/out of full screen

W.contentWindow.visualViewport.onresize=function(e){

 if((W.offsetWidth*W.offsetHeight)==(screen.availWidth*screen.availHeight)){

  // gone full screen    

 } else {

  // exited full screen

 }

};

NB:“W”是您正在操作的框架/窗口。要在您自己的文档中使用此代码,请将“W.contentWindow”替换为“window”。

更多视觉视口信息...

https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport

© www.soinside.com 2019 - 2024. All rights reserved.