c++ - 如何通过 shell 执行最大化启动 Chrome?

标签 c++ windows google-chrome

我正在通过 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/ 为窗口指定默认大小/位置STARTUPINFO 的 dwYSize 成员结构,但我不确定 Chrome 是否尊重这些设置。

关于c++ - 如何通过 shell 执行最大化启动 Chrome?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457817/

相关文章:

windows - 选择多个复选框以执行条件

css - Chrome 忽略了背景位置

macos - 在谷歌浏览器中,某些网页的字体无缘无故发生了变化

c++ - POS 终端应用程序开发 - SDK、编程语言、模式、IDE

c++ - 兼容性和结构填充

c++ - 为什么这段代码会使 VC++ 编译器崩溃?

windows - 为什么不能在 cmd.exe 可执行文件的选项周围加上引号?

javascript - 使用 JQuery 或 JavaScript 覆盖 Chrome 浏览器拼写检查语言

c++ - 使用#ifndef 时具体定义了什么

c++ - 无法编译简单的 C++ 程序,需要说明