Reputation: 4885
I've looked over Stack overflow and seen some posts that almost solve this problem but not really. To be specific I'm referring to an application that is deployed to users who might have 3 or 4 displays and the application would want to remember which one to go to on startup.
I have a WPF application and I want to save what screen it was on when the form does it's OnClose. Then when it loads I want to put my application on that screen.
How can this be done (without hacky Win32 API calls)?
EDIT: In a previous StackO post someone mentioned doing something like:
var screen = System.Windows.Forms.Screen.FromRectangle(
new System.Drawing.Rectangle(
(int)myWindow.Left, (int)myWindow.Top,
(int)myWindow.Width, (int)myWindow.Height));
Once I have screen
it seems that the only thing I can do is check whether the application is either on the primary screen or not. Which is fine...if my users only have two screens, but not good if they have 3+. Is there something better I can do with this resulting screen
object than just check if it's primary.
Upvotes: 2
Views: 800
Reputation: 7719
You can get more infomation on multiple monitors from Screen.AllScreens property
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.allscreens.aspx
Also from memory, I think if you just save your Window Left and Top position and restore them when you load your app, this works with multiple monitors.
Upvotes: 2
Reputation: 7468
I did this previously with a ViewManager class, a custom XML config file and MVVM. Essentially, when OnClose is called, the ViewManager class (A custom class that really just held a collection of also custom ViewInfo instances) is polled to find out which views are currently open and in which order (as they could be tabbed through using Ctrl+Tab) and what the ID of the record was that was being displayed; this app was using Entity Framework to access database data, each application view mapped to either a collection of records or a single record of a specific type.
Anyhow the XML output would contain the name of the View, the Application (there were several portions to the app which I referred to as applications internally) to which it belonged, information about the record that was loaded, etc.
Upon loading the program, the only View that is automatically loaded is the HomeView which potentially contains all other views. ViewManager checks the XML file and loads views based on it's content. This action could be turned off in the options screen so that users where presented with a clean workspace upon entering if they like.
Upvotes: 1