0

I have an annoying program that don't save his position when closed. I've made a small console program that open it and move the window to another position, now i want to save the position when the program close, how i can intercept the closing message so i can save the position before the window destroy itself ?

k0tt
  • 76
  • 1
  • 7
  • 1
    https://msdn.microsoft.com/en-us/library/windows/desktop/dd373640%28v=vs.85%29.aspx (example: http://blogs.msdn.com/b/oldnewthing/archive/2011/10/26/10230020.aspx) – chris Mar 27 '15 at 21:53
  • "*I have an annoying program that don't save his position when closed.*" Most apps **do not** save their positions. Apps have to be specifically coded to do that. – Remy Lebeau Mar 28 '15 at 05:10
  • Expanding on Remy's comment, if an application doesn't store its window position on exit, what are the chances that it will load this information on startup? And where from? Windows 7 introduced a number of shortcuts to quickly position windows. Maybe one of the [Windows 7 logo key Keyboard Shortcuts](http://shortcutmania.com/Windows-7-logo-key-Keyboard-Shortcuts.htm) can help make this application's window positioning less annoying. – IInspectable Mar 28 '15 at 11:46
  • @IInspectable: From the `STARTUPINFO`. In fact, the Win32 API *forces* apps to the rectangle passed from the caller, because so many of them failed to respect it. – Ben Voigt Mar 29 '15 at 03:27
  • @Ben: This assumes that `STARTF_USEPOSITION | STARTF_USESIZE` is set. – IInspectable Mar 29 '15 at 19:21
  • @IInspectable: Given that k0tt's goal is to restore the remembered window location, I think we can assume he will be passing the appropriate flags. – Ben Voigt Mar 29 '15 at 19:23

1 Answers1

1

Since you can position the window after launching its app, you obviously have the window's HWND. However, you cannot subclass a HWND across process boundaries, so your app cannot hook the window directly. You would have to either:

  1. remotely inject some code into the launched process to subclass the target window from within the context of its own process, then your subclass has direct access to all of messages that the window receives.

  2. implement a global message hook in a DLL using SetWindowsHookEx() and look at all of the messages that the target HWND receives.

Either way, when your subclass/hook detects a WM_CLLOSE and/or WM_DESTROY message being delivered to the target window, it can communicate that info back to your app using any Inter-Process Communication (IPC) mechanism of your choosing - named pipe, named event, socket, mailslot, window message, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770