@@ -68,10 +68,16 @@ bool WriteRegistryInt (LPCWSTR pFolder, LPCWSTR pEntry, int val);
6868#define PREF_TOP L" Top"
6969#define PREF_WIDTH L" Width"
7070#define PREF_HEIGHT L" Height"
71+ #define PREF_RESTORE_LEFT L" Restore Left"
72+ #define PREF_RESTORE_TOP L" Restore Top"
73+ #define PREF_RESTORE_RIGHT L" Restore Right"
74+ #define PREF_RESTORE_BOTTOM L" Restore Bottom"
75+ #define PREF_SHOWSTATE L" Show State"
7176
7277// Window state functions
73- void SaveWindowRect ();
74- void RestoreWindowRect (int & left, int & top, int & width, int & height);
78+ void SaveWindowRect (HWND hWnd);
79+ void RestoreWindowRect (int & left, int & top, int & width, int & height, int & showCmd);
80+ void RestoreWindowPlacement (HWND hWnd, int showCmd);
7581
7682// Program entry point function.
7783int APIENTRY wWinMain (HINSTANCE hInstance,
@@ -308,29 +314,90 @@ bool WriteRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int val)
308314 return result;
309315}
310316
311- void SaveWindowRect ()
317+ void SaveWindowRect (HWND hWnd )
312318{
313319 // Save position of active window
314- HWND hWnd = GetActiveWindow ();
315- if (hWnd)
320+ if (!hWnd)
321+ return ;
322+
323+ WINDOWPLACEMENT wp;
324+ memset (&wp, 0 , sizeof (WINDOWPLACEMENT));
325+ wp.length = sizeof (WINDOWPLACEMENT);
326+
327+ if (GetWindowPlacement (hWnd, &wp))
316328 {
317- RECT rect;
318- if (GetWindowRect (hWnd, &rect))
329+ // Only save window positions for "restored" and "maximized" states.
330+ // If window is closed while "minimized", we don't want it to open minimized
331+ // for next session, so don't update registry so it opens in previous state.
332+ if (wp.showCmd == SW_SHOWNORMAL || wp.showCmd == SW_SHOW || wp.showCmd == SW_MAXIMIZE)
319333 {
320- WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_LEFT, rect.left );
321- WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_TOP, rect.top );
322- WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_WIDTH, rect.right - rect.left );
323- WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_HEIGHT, rect.bottom - rect.top );
334+ RECT rect;
335+ if (GetWindowRect (hWnd, &rect))
336+ {
337+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_LEFT, rect.left );
338+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_TOP, rect.top );
339+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_WIDTH, rect.right - rect.left );
340+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_HEIGHT, rect.bottom - rect.top );
341+ }
342+
343+ if (wp.showCmd == SW_MAXIMIZE)
344+ {
345+ // When window is maximized, we also store the "restore" size
346+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_LEFT, wp.rcNormalPosition .left );
347+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_TOP, wp.rcNormalPosition .top );
348+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_RIGHT, wp.rcNormalPosition .right );
349+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_BOTTOM, wp.rcNormalPosition .bottom );
350+ }
351+
352+ // Maximize is the only special case we handle
353+ WriteRegistryInt (PREF_WINPOS_FOLDER, PREF_SHOWSTATE,
354+ (wp.showCmd == SW_MAXIMIZE) ? SW_MAXIMIZE : SW_SHOW);
324355 }
325356 }
326357}
327358
328- void RestoreWindowRect (int & left, int & top, int & width, int & height)
359+ void RestoreWindowRect (int & left, int & top, int & width, int & height, int & showCmd)
360+ {
361+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_LEFT, NULL , left);
362+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_TOP, NULL , top);
363+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_WIDTH, NULL , width);
364+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_HEIGHT, NULL , height);
365+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_SHOWSTATE, NULL , showCmd);
366+ }
367+
368+ void RestoreWindowPlacement (HWND hWnd, int showCmd)
329369{
330- GetRegistryInt (PREF_WINPOS_FOLDER, PREF_LEFT, NULL , left);
331- GetRegistryInt (PREF_WINPOS_FOLDER, PREF_TOP, NULL , top);
332- GetRegistryInt (PREF_WINPOS_FOLDER, PREF_WIDTH, NULL , width);
333- GetRegistryInt (PREF_WINPOS_FOLDER, PREF_HEIGHT, NULL , height);
370+ if (!hWnd)
371+ return ;
372+
373+ // If window is maximized, set the "restore" window position
374+ if (showCmd == SW_MAXIMIZE)
375+ {
376+ WINDOWPLACEMENT wp;
377+ wp.length = sizeof (WINDOWPLACEMENT);
378+
379+ wp.flags = 0 ;
380+ wp.showCmd = SW_MAXIMIZE;
381+ wp.ptMinPosition .x = -1 ;
382+ wp.ptMinPosition .y = -1 ;
383+ wp.ptMaxPosition .x = -1 ;
384+ wp.ptMaxPosition .y = -1 ;
385+
386+ wp.rcNormalPosition .left = CW_USEDEFAULT;
387+ wp.rcNormalPosition .top = CW_USEDEFAULT;
388+ wp.rcNormalPosition .right = CW_USEDEFAULT;
389+ wp.rcNormalPosition .bottom = CW_USEDEFAULT;
390+
391+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_LEFT, NULL , (int &)wp.rcNormalPosition .left );
392+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_TOP, NULL , (int &)wp.rcNormalPosition .top );
393+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_RIGHT, NULL , (int &)wp.rcNormalPosition .right );
394+ GetRegistryInt (PREF_WINPOS_FOLDER, PREF_RESTORE_BOTTOM, NULL , (int &)wp.rcNormalPosition .bottom );
395+
396+ // This returns an error code, but not sure what we could do on an error
397+ SetWindowPlacement (hWnd, &wp);
398+ }
399+
400+ ShowWindow (hWnd, showCmd);
334401}
335402
336403
@@ -395,18 +462,22 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
395462 // - window in secondary monitor when shutdown, disconnect secondary monitor, restart
396463
397464 int left = CW_USEDEFAULT;
398- int top = 0 ;
465+ int top = CW_USEDEFAULT ;
399466 int width = CW_USEDEFAULT;
400- int height = 0 ;
401- RestoreWindowRect (left, top, width, height);
402-
403- hWnd = CreateWindow (szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
467+ int height = CW_USEDEFAULT;
468+ int showCmd = SW_SHOW;
469+ RestoreWindowRect (left, top, width, height, showCmd);
470+
471+ DWORD styles = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
472+ if (showCmd == SW_MAXIMIZE)
473+ styles |= WS_MAXIMIZE;
474+ hWnd = CreateWindow (szWindowClass, szTitle, styles,
404475 left, top, width, height, NULL , NULL , hInstance, NULL );
405476
406477 if (!hWnd)
407478 return FALSE ;
408479
409- ShowWindow (hWnd, nCmdShow );
480+ RestoreWindowPlacement (hWnd, showCmd );
410481 UpdateWindow (hWnd);
411482
412483 return TRUE ;
@@ -662,7 +733,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
662733 case WM_CLOSE:
663734 if (g_handler.get ()) {
664735
665- SaveWindowRect ();
736+ HWND hWnd = GetActiveWindow ();
737+ SaveWindowRect (hWnd);
666738
667739 // If we already initiated the browser closing, then let default window proc handle it.
668740 HWND browserHwnd = g_handler->GetBrowser ()->GetHost ()->GetWindowHandle ();
0 commit comments