如何让Chrome在第二个显示器上打开?

7

我目前正在尝试制作一个应用程序,它可以强制在我的第二个显示器上打开Chrome窗口,但我找不到任何使用参数的方法,现在我想知道是否可以使用Delphi来强制将其打开在第二个屏幕或特定像素上?这仅仅是为了我自己和我的电脑而设计的应用程序,因此我可以针对我的情况将代码放入其中。

我目前正在使用以下代码来启动应用程序:

procedure TForm1.BtnClick(Sender: TObject);
begin
 ExecProcess(ChromePath,'',False);
end;

function ExecProcess(ProgramName, WorkDir: string; Wait: boolean): integer;
var
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CreateOK: boolean;
  ExitCode: integer;
  dwExitCode: DWORD;
begin
  ExitCode := -1;

  FillChar(StartInfo, SizeOf(TStartupInfo), #0);
  FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
  StartInfo.cb := SizeOf(TStartupInfo);

  if WorkDir <> '' then
  begin
    CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, Addr(WorkDir[1]),
      false, CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, nil,
      StartInfo, ProcInfo);
  end
  else
  begin
    CreateOK := CreateProcess(nil, Addr(ProgramName[1]), nil, nil, false,
      CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, Addr(WorkDir[1]),
      StartInfo, ProcInfo);
  end;

  { check to see if successful }

  if CreateOK then
  begin
    // may or may not be needed. Usually wait for child processes
    if Wait then
    begin
      WaitForSingleObject(ProcInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcInfo.hProcess, dwExitCode);
      ExitCode := dwExitCode;
    end;
  end
  else
  begin
    ShowMessage('Unable to run ' + ProgramName);
  end;

  CloseHandle(ProcInfo.hProcess);
  CloseHandle(ProcInfo.hThread);

  Result := ExitCode;

end;

我能否在StartInfo.wShowWindow中使用某些东西呢?


Delphi 不一定是我用来做那件事的工具。您是否考虑使用 JavaScript 写一个打开新窗口并移动它到您想要的位置的功能呢? - Rob Kennedy
你可以尝试使用ShellExecuteEx,在SHELLEXECUTEINFO中指定监视器句柄。 - kobik
1个回答

13

Chrome 可以通过命令行选项 --window-position 和 --window-size 指定窗口的位置和大小。详见此页面

示例:

:: Left screen is 1024x768
"C:\chrome.exe" "https://www.example.com/?a=0&b=1" --window-position=0,0  --window-size=1024,768 --user-data-dir="C:\my-chrome1"

:: Right screen is 1280x720
:: Now chrome.exe we need to open in the second screen then we do it as below:
:: we might want to use --kiosk but combination of --kiosk and --window-position wont work so in that case we can use --app


"C:\chrome.exe" --app="https://www.example.com/?a=0&b=1" --window-position=1025,0 --window-size=1280,720 --user-data-dir="C:\my-chrome2"

一切都是正确的。你所要做的就是将Chrome窗口的初始位置设置为第二个监视器上。有关如何在您自己的Delphi应用程序表单中执行此操作的更多信息,请查看以下文章:https://dev59.com/x3VC5IYBdhLWcg3wtzgQ - SilverWarior
谢谢@Nat,这个完美地解决了!我到处都找了,但关于这个问题找不到任何东西,如此简单却如此困难。 - user3464658
如果你想共享会话、密码、cookies等信息,请使用相同的--user-data-dir参数。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接