Multiple Monitors launching
Scenario: I have a dual-monitor setup. When I launch the main program, I need to make sure that it always launches in "Screen 1". I have a button within the program that launches another fullscreen window and I need this to always launch in the next available screen (read: not covering the main program window)
I am using the System.Windows.Forms.Screen class to try to accomplish this and have have had about 80% success. I can successfully test for multiple monitors (which enables the button). When they click it, I can get it to always launch in the second screen. The problem I'm encountering while testing is the main program is launching in Screen 2, so the second window covers it.
Here's what I'd like to accomplish ultimately: I would like to always launch my program in Maximized stated (and disable resizing, minimizing/maximizing is ok, just not double-clicking titlebar to resize) in the primary monitor (where Taskbar is being displayed). Then, when the button within the program is pressed, it launches the new window in the NEXT available monitor in Maximized stated.
Re: Multiple Monitors launching
what happens if you set window2's location to:
Code:
window2.location = new point(window1.location.x + window1.size.width + 20,0)
window2.state = maximized 'not the actual code
window2.show()
?
Justin
Re: Multiple Monitors launching
is there a way to test which screen a certain window resides in?
like, if i used your code (which i haven't tested), if they have 2 monitors and they move the primary program window to the second monitor (on the right), then your code would put it to the right of that one, which would put it where a 3rd monitor WOULD be (off-screen basically)
I need a way to test:
pseudo:
vb Code:
Dim screen() As Screen = Screens.AllScreens
if mainProgramWindow.Screen = screen(0) then 'This isn't testable from what I can see
secondProgramWindow.Screen = screen(1)
secondProgramWindow.Show
Else
secondProgramWindow.Screen = screen(0)
secondProgramWindow.Show
End if
Re: Multiple Monitors launching
I think this would work:
Code:
screen = screen.AllScreens(0)
isn't that the default monitor?
Justin
Re: Multiple Monitors launching
that would work for what? I already have that code.
My application will always launch on whatever the primary monitor is. This is fine. I need a way to TEST what monitor it is displaying in though, as the primary monitor may be on the left, or it may be on the right. (in a 2 monitor setup)
My project should be able to run on a limitless amount of monitors.
Re: Multiple Monitors launching
Umm you can get the bounds of the screen?
if you know what the x,y width and height of each screen is, you know where to put it.
Justin
Re: Multiple Monitors launching
having trouble getting my mind around this one.
Let's assume i have 4 monitors (all the same size and resolution) *this will be a real scenario during testing*
Let's also assume the far left monitor is the primary monitor and each consecutive monitor to the right of that is 2, 3, and 4 respectively.
When I launch my program, I would expect it to show up in the far left monitor (easy enough). The "Spawn" button within my program should launch the next window in the second monitor. If I click it again, it should launch the next window in the 3rd monitor etc. (this should work regardless of number of monitors)
Now, this is pretty easy to test for, I could capture the width and height of the primary screen and test whether my form's location is within these bounds. if so, launch it in the next available screen.
..ok, typing that out actually helped me think through this.
It's weird though, my computer at work (2 monitors) performs differently. The monitor on the left displays "1" and the monitor on the right displays "2" when clicking "Identify" in Display Properties. However, when launching my program from within Visual Studio to test, it launches the program in the right monitor, monitor "2". This causes the spawned window to open up on top of it (because right now i hard-coded Screen(1).)
How can I ensure that my program will ALWAYS open on the first AVAILABLE screen (not necessarily the furthest left or whatever)?
Re: Multiple Monitors launching
Screen.Primary lets you know which is the main screen.
if say your primary screen is at index... 2.. and you have 0 - 4 monitors in total.
then you know that you have 2 to the left, and 2 to the right.
so you could draw to screen at index 2 first, then 0,1,3,4.
Just remember where the last form is loaded so you can reset your x,y to reuse that position.
Justin
Re: Multiple Monitors launching
Quote:
How can I ensure that my program will ALWAYS open on the first AVAILABLE screen (not necessarily the furthest left or whatever)?
Wouldn't the System.Windows.Forms.Screen.AllScreens Class have anything you need?
1 Attachment(s)
Re: Multiple Monitors launching
here's a screenshot of the application runnin on my dev environment. the right screen is my primary and it correctly spawns new window in the left monitor, not covering the primary.
i'll look at that Screens.Primary property. I didn't see that one earlier.
for argument's sake, how would i FORCE the main program to launch in a specific window? can this be set before the form is loaded or what event should this be set in?
Re: Multiple Monitors launching
You can always set the Location.X and Location.Y of the form before you show it.
So you could just keep track of what monitors you use when you show them, and then remove that monitor from the 'remember' list when that form closes.
so if the primary is avail. then you have only ran the main form, so set the x,y to that screen. else, find the first avail. monitor in the screen.allscreens, set the new spawn form's x,y = that screen, add that screen to your remember list so next time you'll go to the next avail. one.
Justin
Re: Multiple Monitors launching
Quote:
Originally Posted by
MonkOFox
You can always set the Location.X and Location.Y of the form before you show it.
ahh that's what i'm trying to do, but doesn't seem to work in the Load Event. I'm not sure where I should actually put the code to set the X and Y prior to it being shown.
The rest seems doable. I'll give it a shot.
Re: Multiple Monitors launching
Yeah I didn't think about that. It will work for any new forms you create while the program is running, but IF the first form always pops up in the primary screen, then you're still good, just loop through the screens and if it hasn't been used yet, and isn't the primary, then you should be good.
If that isn't the case, then you could probably do some logic location programming in the designer.vb of the application startup form, to ensure it gets loaded into the primary screen.
Keep us posted, want to see if you get it working : ),
Justin
Re: Multiple Monitors launching
Make sure you set the form's "StartPosition" to manual just in case.
Re: Multiple Monitors launching
cool, you got me thinking in a different approach and here's what i'm doing:
vb Code:
'Number of Monitors Client has connected
Dim myScreens() As Screen = Screen.AllScreens
Dim numScreens As Integer = myScreens.Length
Dim availableScreens As Integer = myScreens.Length
Every time I call my "LaunchAnotherWindows" Sub, I subtract 1 as well. Here's that sub:
vb Code:
Public Sub LaunchInNewMonitor()
availableScreens -= 1
'Set flag to show that multiple monitors are now being used
multipleMonitors = True
'Create a New Parent Form
Dim secondScreenParent As New Form
'Set its Properties
With secondScreenParent
.ShowInTaskbar = True
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.Width = myScreens(numScreens - availableScreens).WorkingArea.Width
.Height = myScreens(numScreens - availableScreens).WorkingArea.Height
.IsMdiContainer = True
.StartPosition = FormStartPosition.Manual
.Location = myScreens(numScreens - availableScreens).Bounds.Location
.Show()
End With
ShowCamerasForm(16, "00001", 1, secondScreenParent)
End Sub
I'll add an addition statement in the closing event.
I think this should work just fine! Im anxious to test it, but getting access to more than 2 monitors may prove to be difficult lol. It's working on 2 though!
EDIT: removed the -1 from the main form load. it was throwing index out of range. i now disable the button once all screens are filled.
Re: Multiple Monitors launching
Awesome man! Glad it's working for you : ). Theoretically (to me anyways) it should work on 2 monitors as well as 24.
But you'll just have to test it on more than 2 to see the outcome.
Grats again!
Justin