1

I am launching Chrome with the app="http://..." parameter (a Chrome application shortcut) via C++. Now it seems to open with a size of roughly 400x800 which is crazy. I'd like to open it maximized or at least have it remember the size.

Is there a way to achieve this?

Tower
  • 98,741
  • 129
  • 357
  • 507

2 Answers2

4

If you don't mind using the default browser (which, in my opinion, is the best option) instead of forcing the use of Chrome, you can simply open your URL with ShellExecute specifying that you want the window to be maximized:

#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?

Well, in this case it's better to use 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
}

Notice that you can try to specify a default size/posizion for the window using the dwX/dwY/dwXSize/dwYSize members of the STARTUPINFO structure, but I'm not sure if Chrome respects these settings.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I *must* open Chrome, and I have its path known in a variable. I also need to specify one parameter. Is this a problem? – Tower Sep 17 '11 at 21:58
  • You could re-adapt this code to work with it, but if you have a specific path to it it's better to use `CreateProcess` and keep the shell out of it. I'll post the code in 2 mins. – Matteo Italia Sep 17 '11 at 22:01
  • I'm not getting it to work. I can make it work `SW_SHOWMINIMIZED` etc, but with `SW_SHOWMAXIMIZED` it just does not work. Nothing happens. If I try to resize afterwards the process creation I get access violations. Code: http://pastie.org/private/la9i5apucfy8b8psuxw8pg – Tower Sep 17 '11 at 22:47
  • Your code is filled with errors: first of all, I *explicitly wrote* that the second parameter *must be* a writable buffer, and the fact that the parameter is non-const is exactly for that reason. You shouldn't just put a `const_cast` there, but pass a modifiable `char *` or copy the `const char *` you have into a buffer. Then, into the `if`, you are leaking handles by not closing them. But most importantly, it makes no sense to cast `hProcess` to an `HWND`: it's a *process handle*, not a *window* handle, it's obvious that you get access violations! – Matteo Italia Sep 17 '11 at 22:55
  • @Matteo: Thank you for your feedback. Here is the updated code: http://pastie.org/private/apyb0poqqmoj3koo25dq Chrome does not seem to detect the SW_SHOWMINIMIZED/MAXIMIZED values. Is there something that you would recommend? – stan Sep 17 '11 at 23:27
  • @Stanislav: there are still all the errors I said before besides the wrong usage of the handle. Still, if Chrome does not listen to `SW_SHOWMAXIMIZED`, you should try to get the top-level window from the process handle you have, which is more or less summarized in [this](http://stackoverflow.com/questions/2620409/getting-hwnd-of-current-process/2620522#2620522) reply. – Matteo Italia Sep 17 '11 at 23:40
-1

--start-maximized should do the trick. Taken from http://peter.sh/experiments/chromium-command-line-switches/ Haven't tested it myself though..

kfir
  • 331
  • 4
  • 12