Introduction
This article describes a simple approach to capturing and displaying screen-related information. Figure 1 of this document demonstrates some of the information that may be captured through the use of the System.Windows.Forms.Screen class. This class makes it very easy to capture screen-related information; however, making alterations to the screen resolution still requires reliance upon calls made through the API. Of course, whether or not it is ever a good idea to change the user's screen resolution is debatable; however, in some instances, it is still necessary (e.g., for displaying certain graphics properly).
Figure 1. Displaying Screen-Related Information
Getting Started
There is a single Win Forms application included with this download. You may open the project and examine the project if you wish; however, the code is quite simple and will be described in the following sections of this document. All examples were written using Visual Studio 2005 and are in C#; the same code could also be used in earlier versions of Visual Studio. The project does not use any references aside from the defaults; there is only a single main form and program file included in the solution.
Figure 2. The Project in the IDE's Solution Explorer
The Main Form
The application contains a single form; the form contains a collection of text boxes and the contents of the text boxes are all set upon form load. The text boxes are used to display the device name, the screen bounds, whether or not the device is the primary display, the working area of the display, and the bits pixel value associated with the display under the current configuration.
The information regarding the screen is captured through the use of the System.Windows.Forms.Screen classes. The code for the project is very simple, and it is shown entirely in the following.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ScreenInfo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen;
txtBitsPerPixel.Text = scr.BitsPerPixel.ToString();
txtBounds.Text = scr.Bounds.ToString();
txtName.Text = scr.DeviceName.ToString();
txtType.Text = scr.Primary.ToString();
txtWorkArea.Text = scr.WorkingArea.ToString();
}
private void btnOK_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The imports used with this project include only the defaults. In the form load call, an instance of the System.Windows.Forms.Screen class is created as the primary screen. It is also possible to pull up a collection of all of the screens if the user is equipped with multiple display devices. After the screen object is created, properties from this screen object are used to set the values displayed in the text boxes included in the form.
Summary
This article demonstrates a simple approach to capturing information regarding the user's display hardware through the use of the System.Windows.Forms.Screen class. Such information may be useful in some applications where it may be necessary to check the screen resolution prior to displaying certain types of graphics or animations or may be useful if the developer needs to manually resize or position screen elements as a function of the display hardware size and resolution specifications.