@@ -68,10 +68,16 @@ bool WriteRegistryInt (LPCWSTR pFolder, LPCWSTR pEntry, int val);
68
68
#define PREF_TOP L" Top"
69
69
#define PREF_WIDTH L" Width"
70
70
#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"
71
76
72
77
// 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);
75
81
76
82
// Program entry point function.
77
83
int APIENTRY wWinMain (HINSTANCE hInstance,
@@ -308,29 +314,90 @@ bool WriteRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int val)
308
314
return result;
309
315
}
310
316
311
- void SaveWindowRect ()
317
+ void SaveWindowRect (HWND hWnd )
312
318
{
313
319
// 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))
316
328
{
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)
319
333
{
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);
324
355
}
325
356
}
326
357
}
327
358
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)
329
369
{
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);
334
401
}
335
402
336
403
@@ -395,18 +462,22 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
395
462
// - window in secondary monitor when shutdown, disconnect secondary monitor, restart
396
463
397
464
int left = CW_USEDEFAULT;
398
- int top = 0 ;
465
+ int top = CW_USEDEFAULT ;
399
466
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,
404
475
left, top, width, height, NULL , NULL , hInstance, NULL );
405
476
406
477
if (!hWnd)
407
478
return FALSE ;
408
479
409
- ShowWindow (hWnd, nCmdShow );
480
+ RestoreWindowPlacement (hWnd, showCmd );
410
481
UpdateWindow (hWnd);
411
482
412
483
return TRUE ;
@@ -662,7 +733,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
662
733
case WM_CLOSE:
663
734
if (g_handler.get ()) {
664
735
665
- SaveWindowRect ();
736
+ HWND hWnd = GetActiveWindow ();
737
+ SaveWindowRect (hWnd);
666
738
667
739
// If we already initiated the browser closing, then let default window proc handle it.
668
740
HWND browserHwnd = g_handler->GetBrowser ()->GetHost ()->GetWindowHandle ();
0 commit comments