我正在通过 C++ 使用 app="http://..."
参数(Chrome 应用程序快捷方式)启动 Chrome。现在它似乎以大约 400x800 的大小打开,这太疯狂了。我想最大化打开它,或者至少让它记住大小。
有什么办法可以实现吗?
最佳答案
如果您不介意使用默认浏览器(我认为这是最好的选择)而不是强制使用 Chrome,您只需使用 ShellExecute
打开您的 URL。指定您希望窗口最大化:
#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 */
}
I must open Chrome, and I have its path known in a variable. I also need to specify one parameter. Is this a problem?
嗯,在这种情况下最好使用 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
}
请注意,您可以尝试使用 dwX
/dwY
/dwXSize
/ 为窗口指定默认大小/位置
成员结构,但我不确定 Chrome 是否尊重这些设置。STARTUPINFO
的 dwYSize
关于c++ - 如何通过 shell 执行最大化启动 Chrome?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457817/