批处理在不同屏幕上打开程序

我想运行两个程序 一个直接打开chrome在主屏全屏显示 一个直接打开edge在扩展屏上全屏显示,好像是因为我只有一个独显,设备id相同,所以无法使用--display ,但是为什么我的--window-position=0,0 --window-size=2560,1600 也没有生效 我主屏25601600 扩展屏是38402160,平行左右排列,请教一下为啥没有生效,总是在一个屏幕出现两个网页

@echo off
setlocal enableDelayedExpansion

rem 切换到 Python 脚本目录,启动 Python 应用程序
cd ".\Large screen\audio-painter\flask"
start cmd /k "python app.py"

rem 等待 1 秒钟,以确保 Python 应用程序已经启动
choice /t 1 /d y /n >nul

rem 切换到大屏脚本目录,启动大屏应用程序
cd "..\..\textDrawing-web\coart-web\src"
start cmd /k "npm run dev"

rem 等待 10 秒钟,以确保大屏应用程序已经启动
choice /t 10 /d y /n >nul

set "url=http://localhost:8080"
set "width=3840"
set "height=2160"
set "xpos=2560"
set "ypos=-1600"

@REM LEN61B0 CSO1600
rem 枚举所有的显示器并找到扩展屏的设备ID
for /f "tokens=2 delims==." %%i in ('wmic path Win32_PnPEntity where "PNPClass='Monitor'" get DeviceID /value ^| findstr /r [0-9]') do (
    set "monitorid=%%i"
    @REM echo %%i
    @REM pause
    echo !monitorid! | find /i "CSO1600" >nul
    if not errorlevel 1 (
        set "display=\\.\!monitorid!"
        echo 找到扩展屏:%display%
        goto display_found
    )
)


rem 如果找到扩展屏则在扩展屏上全屏打开 Chrome
goto end

:display_found
@REM start "" "chrome" --new-window  --kiosk -- --window-position=%xpos%,%ypos% --window-size=%width%,%height% --display=\\.\CSO1600 "%url%"
start msedge  --new-window  --kiosk "%url%"  --window-position=0,0 --window-size=2560,1600 


%切换小屏路径%
cd ..\..\..\..\small screen\textDrawing\src
start  cmd /k "npm run dev"

rem 等待 10 秒钟
choice /t 10 /d y /n >nul

start msedge --new-window --kiosk "http://localhost:8081"  --edge-kiosk-type=fullscreen --window-position=2560,0 --window-size=3840,2160 
:end

引用new bing部分回答作答:
您在命令行中使用的参数 --window-position 和 --window-size 可能不起作用的原因是,Chrome 和 Edge 可能会忽略这些参数,因为它们被视为启动标志而不是应用程序窗口的大小和位置。

另一个问题是,您可能需要使用不同的 --display 参数来在不同的屏幕上启动应用程序。如果您的两个屏幕具有相同的设备 ID,则可以尝试使用屏幕的位置坐标来替代 --display 参数。

例如,对于您的情况,可以尝试以下命令来在不同的屏幕上启动 Chrome 和 Edge:

:: Start Chrome on the primary screen
start chrome --new-window --kiosk --app=%url% --window-position=0,0 --window-size=2560,1600

:: Start Edge on the secondary screen
start msedge --new-window --kiosk --app=%url% --window-position=2560,0 --window-size=1280,720

这仅适用于具有不同设备 ID 的屏幕,因为这样才能在命令行中使用 --display 参数。如果您的两个屏幕具有相同的设备 ID,则需要使用位置坐标来替代 --display 参数。例如:

:: Start Chrome on the primary screen
start chrome --new-window --kiosk --app=%url% --window-position=0,0 --window-size=2560,1600

:: Start Edge on the secondary screen
start msedge --new-window --kiosk --app=%url% --window-position=-1280,0 --window-size=1280,720

这将在两个屏幕上启动两个应用程序,一个在左侧的主屏幕上,一个在右侧的扩展屏幕上。请注意,位置坐标的符号必须与屏幕的位置相对应。在这种情况下,我们将 xpos 设为 -1280,因为扩展屏幕位于主屏幕的右侧,所以应使用负值。

您的问题可能是由于使用了错误的参数导致的。--window-position 和 --window-size 是针对 Chrome 的指令,无法在 Microsoft Edge 上运行。可以尝试使用 -h 或 --help 命令获取 Microsoft Edge 支持的命令行选项示例,并根据您的需求进行调整。
例如,以下是在 Microsoft Edge 上打开网页并在扩展屏幕上全屏显示的示例:

start msedge.exe "https://www.example.com" --kiosk --display \\.\DISPLAY2


其中 \.\DISPLAY2 是扩展屏幕的设备 ID。您可以使用 Windows API 获得所有显示器的设备 ID,从而找到扩展屏幕的设备 ID。另外,如果您要在主屏上全屏显示,只需要省略 --display 参数即可。
另外,使用 start 命令启动应用程序时,可以使用 /D 参数来指定应用程序的工作目录,如:


start /D "C:\MyApp\" MyApp.exe

这样可以确保应用程序在正确的工作目录中开始运行。

下面是一些可能的优化,由于优化方案需要根据具体的场景和需求进行设计,这里提供一个简单的示例代码,主要是通过参数化的方式来传递显示器的位置和大小,并使用相对路径和变量来避免路径问题:


@echo off
setlocal enableDelayedExpansion

rem 设置常用的路径变量
set "large_screen_path=.\Large screen\audio-painter\flask"
set "text_drawing_path=..\..\textDrawing-web\coart-web\src"
set "small_screen_path=..\..\..\..\small screen\textDrawing\src"

rem 启动 Python 应用程序
pushd "%large_screen_path%"
start cmd /k "python app.py"
popd

rem 启动大屏应用程序
pushd "%text_drawing_path%"
start cmd /k "npm run dev"
popd
choice /t 10 /d y /n >nul

rem 在扩展屏上全屏打开 Chrome
set "display=\\.\CSO1600"
start msedge --new-window --kiosk --window-position=2560,-1600 --window-size=3840,2160 --display="%display%" "http://localhost:8080"

rem 启动小屏应用程序
pushd "%small_screen_path%"
start cmd /k "npm run dev"
popd
choice /t 10 /d y /n >nul

rem 在主屏幕上全屏打开浏览器
set "main_display=\\.\DISPLAY1"
start msedge --new-window --kiosk --window-position=0,0 --window-size=2560,1440 --display="%main_display%" "http://localhost:8081"

这个脚本启动 Python 和 npm 应用程序,并在扩展屏和主屏幕上分别打开浏览器,显示不同的网页。其中,扩展屏的位置为 (2560, -1600),大小为 (3840, 2160);主屏幕的位置为 (0, 0),大小为 (2560, 1440)。对于路径问题,使用了相对路径和常用的路径变量来减少代码中的重复性。

内容来源与ChatGpt4及newbing和百度:


你可以尝试使用Windows API来打开程序并指定显示器。具体步骤如下:

  1. 获取所有显示器的句柄。

你可以使用 EnumDisplayMonitors 函数来获取所有显示器的句柄。该函数需要一个回调函数作为参数,用于处理每个显示器的信息。

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    std::vector<HMONITOR>* monitors = reinterpret_cast<std::vector<HMONITOR>*>(dwData);
    monitors->push_back(hMonitor);
    return TRUE;
}

std::vector<HMONITOR> GetMonitors()
{
    std::vector<HMONITOR> monitors;
    EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
    return monitors;
}
  1. 打开程序并指定显示器。

你可以使用 CreateProcess 函数来打开程序,并使用 SetWindowPos 函数来将程序窗口移动到指定的显示器。

void OpenProgramOnMonitor(const std::wstring& programPath, int monitorIndex)
{
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;

    if (CreateProcess(programPath.c_str(), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        std::vector<HMONITOR> monitors = GetMonitors();
        if (monitorIndex >= 0 && monitorIndex < monitors.size())
        {
            HMONITOR hMonitor = monitors[monitorIndex];
            MONITORINFO mi = { sizeof(mi) };
            if (GetMonitorInfo(hMonitor, &mi))
            {
                HWND hWnd = pi.hProcess;
                SetWindowPos(hWnd, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, 
                    mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top, 
                    SWP_SHOWWINDOW);
            }
        }
    }
}

使用示例:

OpenProgramOnMonitor(L"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", 0); // 在主屏打开Chrome
OpenProgramOnMonitor(L"C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe", 1); // 在扩展屏打开Edge

祝您问题迎刃而解

该回答参考ChatGPT:
可能是因为你的命令行参数没有正确指定要打开的显示器。你可以尝试使用一些第三方工具来管理窗口,并将它们分别放置在主屏幕和扩展屏幕上。例如,您可以尝试使用类似于 displayfusion 这样的窗口管理器来调整程序窗口的位置和大小。或者,你可以使用 -new-window 参数在两个不同的Chrome/Edge实例中打开这些浏览器窗口。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
首先,关于 --display 参数无法使用的问题,确实是因为你只有一个显卡,Windows 操作系统只会为一个显卡分配唯一的 \\.\DISPLAY1 标识符,在这种情况下,如果你想在不同的显示器(主屏和扩展屏)上显示不同的内容,你需要手动指定每个显示器的位置和大小,而不能简单地依赖 --display 参数。

至于你的 --window-position--window-size 参数没有生效的问题,有可能是因为 start 命令的语法或参数格式不正确。下面是一个使用 --window-position--window-size 参数打开 Chrome 时工作正常的例子:

start chrome --new-window --kiosk "https://www.google.com" --window-position=3840,0 --window-size=3840,2160

这个命令可以在扩展屏的右边全屏打开 Chrome,而在主屏上则不会显示。你需要根据自己的情况调整 --window-position 参数的值。

如果你要在 Edge 上使用类似的参数,你需要使用 --edge-window-features 参数来指定窗口位置和大小。下面是一个可以在扩展屏上全屏打开 Edge 的例子:

start msedge --new-window --kiosk "https://www.google.com" --edge-window-features="left=3840,top=0,width=3840,height=2160"

注意:在扩展屏上全屏打开 Edge 只有在 Edge 88 或更高版本中才受支持。

所以,在你的批处理脚本中,你需要使用类似下面的命令来启动 Chrome 和 Edge:

rem 在主屏上打开 Chrome
start chrome --new-window --kiosk "https://www.google.com" --window-position=0,0 --window-size=2560,1600

rem 在扩展屏上打开 Edge
start msedge --new-window --kiosk "https://www.google.com" --edge-window-features="left=2560,top=0,width=3840,height=2160"

同时,请注意:如果你在批处理脚本中使用了 start 命令启动不同的程序,你需要使用 timeoutping 等命令来等待程序启动完全,否则后续的命令可能会在程序还没有完全启动的情况下就被执行。例如:

rem 等待 Python 应用程序启动
timeout /t 1

rem 等待大屏应用程序启动
timeout /t 10

综上所述,你的完整批处理脚本应该类似于下面的代码:

@echo off

rem 启动 Python 应用程序
cd ".\Large screen\audio-painter\flask"
start cmd /k "python app.py"
timeout /t 1

rem 启动大屏应用程序
cd "..\..\textDrawing-web\coart-web\src"
start cmd /k "npm run dev"
timeout /t 10

rem 在主屏上打开 Chrome
start chrome --new-window --kiosk "https://www.google.com" --window-position=0,0 --window-size=2560,1600

rem 在扩展屏上打开 Edge
start msedge --new-window --kiosk "https://www.google.com" --edge-window-features="left=2560,top=0,width=3840,height=2160"

rem 启动小屏应用程序
cd "..\..\..\..\small screen\textDrawing\src"
start cmd /k "npm run dev"
timeout /t 10

rem 在扩展屏上打开另一个 Edge 窗口
start msedge --new-window --kiosk "http://localhost:8081" --edge-window-features="left=6400,top=0,width=2560,height=1600"

注意:以上代码只是示例,你需要根据你自己的情况调整参数和值。
如果我的回答解决了您的问题,请采纳!