如何启动Chrome最大化通过shell执行

How to launch Chrome maximized via shell execution?

本文关键字:最大化 shell 执行 Chrome 何启动 启动      更新时间:2023-10-16

我通过c++启动带有app="http://..."参数(Chrome应用程序快捷方式)的Chrome。现在它似乎以大约400x800的大小打开,这太疯狂了。我想把它打开到最大或者至少让它记住大小。

有办法做到这一点吗?

如果你不介意使用默认浏览器(在我看来,这是最好的选择)而不是强制使用Chrome,你可以简单地打开你的URL与ShellExecute指定你想要最大化的窗口:

#include <windows.h>
#include <Shellapi.h>
// requires linking towards Shell32.lib
// ...
if(ShellExecute(NULL, "open", "http://www.stackoverflow.com", NULL, NULL, SW_SHOWMAXIMIZED)<=32)
{
    /* an error occurred */
}

我必须打开Chrome,并且我在变量中知道它的路径。我还需要指定一个参数。这是个问题吗?

在这种情况下,最好使用CreateProcess:

#include <windows.h>
// ...
// Assuming that the path to chrome is inside the chromePath variable
// and the URL inside targetURL
// Important: targetURL *must be* a writable buffer, not a string literal
// (otherwise the application may crash on Unicode builds)
PROCESS_INFORMATION processInformation;
STARTUPINFO startupInfo;
memset(&processInformation, 0, sizeof(processInformation));
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.wShowWindow = SW_SHOWMAXIMIZED;
BOOL result= CreateProcess(chromePath, targetURL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation);
if(result)
{
    WaitForSingleObject( processInformation.hProcess, INFINITE );
    CloseHandle( processInformation.hProcess );
    CloseHandle( processInformation.hThread );
}
else
{
    // An error happened
}

请注意,您可以尝试使用STARTUPINFO结构的dwX/dwY/dwXSize/dwYSize成员指定窗口的默认大小/位置,但我不确定Chrome是否尊重这些设置。

——start-maximized应该可以做到。摘自http://peter.sh/experiments/chromium-command-line-switches/不过我自己还没有测试过……