Serg Popov Serg Popov - 1 month ago 18
C++ Question

How to remember the monitor application is located at?

I am writing a C++ mfc application, which I want to remember its position. It should open it's windows exactly where they were before closing them. On opening of a window I would read the data from file or register and restore the windows's desktop on which it was closed, the coordinates of left top point and window size.

There is no problem with coordinates and size, but what is the best way to keep track of the display I have a window at?

Number of display won't work, because there is no way to get a monitor by number and the number of a monitor in WinAPI.

HMONITOR value might fit, according to msdn: "A physical display has the same HMONITOR as long as it is part of the desktop." (https://msdn.microsoft.com/en-us/library/dd144968(v=vs.85).aspx). But if I reboot the PC, will HMONITOR value I saved remain consistent?

Answer

You can call SetWindowPlacement() when your application is closing to get the size and location of your window (including if it is maximized, restored, etc.). Save this information somewhere (the registry, application data, etc.), and when your program starts up next time read this information in and call GetWindowPlacement() to restore the window to its previous location.

In addition, you will probably want to verify that the new location of the window is visible, and if it isn't to move it so that it is visible again. This can occur if a different number of monitors are set up (such as if you aren't using an external display with a laptop).

Comments