7

I'm currently trying to make an app that forces a Chrome window to open on my second monitor but I can't find anyway to do it using arguments, now I'm wondering if I can somehow use Delphi to force it to open on the second screen or a specific pixel? This is solely an app for myself and my PC so I can put the code in specific for my case.

I'm currently using this bit of code to start the app

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;

Can I somehow use something in StartInfo.wShowWindow maybe?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
user3464658
  • 229
  • 1
  • 5
  • 14
  • Delphi isn't necessarily the tool I'd use for that. Have you considered writing JavaScript to open a new window and move it where you want? – Rob Kennedy Jan 15 '15 at 04:39
  • You could try [`ShellExecuteEx`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154%28v=vs.85%29.aspx) specifying the monitor handle in [`SHELLEXECUTEINFO`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb759784%28v=vs.85%29.aspx). – kobik Jan 15 '15 at 09:11

1 Answers1

13

Chrome allows you to pass a position and size on the command line with --window-position and --window-size I believe. Check out this page for details.

Example:

:: 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"
Nat
  • 5,414
  • 26
  • 38
  • is wright. All you have to do is set initial position of the Chrome window to be on the second monitor. For more information how you can do this with your own Delphi application forms check this article http://stackoverflow.com/questions/206400/start-program-on-a-second-monitor – SilverWarior Jan 15 '15 at 07:20
  • Thank you @Nat, this worked perfectly! I was looking everywhere but couldn't find anything regarding this, so simple yet so hard. – user3464658 Jan 15 '15 at 17:22