views:

38

answers:

2

Fairly straightforward question: How can I tell a VB6 application to remember what display it was closed on, and then when the application is launched again, to display the main form on that monitor?

+3  A: 

Why don't you just save the location and size of your window before exiting, and then restores during startup? Do you need to position by monitor?

Onkelborg
I do. The application always starts up on the primary display, even if it was closed on a secondary one.
SpikeX
No, that's not what I said. I said you can just save the location of your form, and then relocate the form on startup. That's entirely different from saving what monitor it's displayed on, and what I want to know is if that really is a requirement or not.
Onkelborg
It doesn't really matter, the end result I need is that the form is opened in the exact same way that it was closed (maximized or restored, and on the same display in the same position). I don't know how to do that, or if I need special code to detect which display it is currently on, or not.
SpikeX
The only thing you have to do is to save the position and size of your form (Me.Left, Me.Top, Me.Width and Me.Height) and the window state (Me.WindowState) before exiting, try using SaveSetting. Then, upon startup, read the settings with GetSetting and restore them to your form
Onkelborg
You were both right, but @Onkelborg was right first. Simply saving those 5 values on Form Load/Unload worked perfectly. I wasn't aware this solution would work with a multi-monitor setup, but apparently it does (see @Chris J's post for the explanation of why).
SpikeX
+3  A: 

You don't need to be "aware" of all monitors. In this instance, you just be multi-monitor agnostic.

For the sake of this example, assume you have a two monitor setup, the primary to the left of the secondary, and both monitors have a resolution of 1024x768.

Rather than think of your app running in a multi-monitor environment, think of it as an application running on a single desktop that's 2048x768.

In this case, all you need to do is determine where on the desktop it sits, and Windows takes care of the rest. You don't need to worry which monitor it's on: if you set the Window to appear within the box (0,0)-(1023,767) then it appears on the primary. If the co-ords are within the box (1024,0)-(2047,767) then it appears on the secondary monitor.

It's simply a case of asking Windows to tell you the size and position of the application window. And for that you need to look at GetWindowRect() and SetWindowPos(). There's an example of how to use GetWindowRect() behind this link. (VB might have a native way of getting this without resorting to the Win32 API, but I don't have VB6 available in front of me to test with).

Chris J
VB6 has Me.Top/Left/Height/Width (where "Me" is the current form), so yes, this is correct.
SpikeX
It gets trickier though to be tolerant of configuration changes: subsequent runs where the 2nd monitor is gone, or logically "moved" relative to the first, etc.
Bob Riemersma
+1 Bob. BTW Karl Peterson wrote an article about multiple monitors in VB6. His articles are always worth a read. http://vb.mvps.org/articles/vsm20090302.pdf
MarkJ