Getting screen information comes from the Screen class, which is a part of the System.Windows.Forms namespace.
In this class you are able to gather all the information you need about available screens including the device names, bounds, work area and how many screens are available.
You can explore MSDN for all of the information available to you, but I will go through the most prominent here.
Screen Information
For each of the monitors in the array you are able to get the following information:
Bits Per Pixel: The pixel bit depth.
Bounds: This will return a rectangle, the size being that of the resolution of the screen.
Device Name: The name of the screen.
Primary: A boolean value as to whether the screen is the primary screen or not.
Working Area: The area of the screen minus the taskbar or any other docked windows or toolbars.
Screen Count
Screen.AllScreens contains an array of screen objects representing all of the available screens on your system. To get the count of these you can use
Screen.AllScreens.Count(); |
or
Screen.AllScreens.Length; |
Current Screen
You can retrieve whichever screen your Windows Form is on by using this code:
string CurrentScreenName = Screen.FromControl(this).DeviceName; |
Or you can retrieve a screen object:
Screen CurrentScreen = Screen.FromControl(this); |
You could also retrieve which screen the mouse is on:
Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y)); |
Make Use Of It
There are many ways you can use this information to make your application multi-monitor friendly, here is an example:
Let’s say you want your form to show up in the the middle of the second available screen. So, this could be the second monitor in a two monitor setup:
this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2); this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2); |
Warnings
- If you are saving and loading the position of your application in a multi screen setup, be sure to check each time that you set the window position that the screens are still available. Otherwise you may be setting your window position to a place where the user will never be able to see or interact with it if they remove the other screens.
'C#' 카테고리의 다른 글
[C#]듀얼모니터 이용하기 (0) | 2012.08.06 |
---|---|
[C#][VisualBasic]VB 2010 Resize a form without border (Original mode) - 크기조절 폼 크기 리사이징 (0) | 2012.08.06 |
폼에 그림자 넣기 (0) | 2012.08.06 |
Custom ComboBoxes with Advanced Drop-down Features(커스텀 콤보박스) (0) | 2012.08.06 |
[Tip] C# ListView Detail보기 Item(항목) 간격(Row, Height) 조절하기. VisualStudio.Net / 개발 list c# 리스트 (0) | 2012.08.06 |