Hi guys,
I want to introduce you to two .NET classes used to identify the screen resolution and also to handling the change of screen resolution:

  • SystemParameters: allow you to retrive a lot of useful system information, including the screen size. In the specific case the screen size is SystemParameters.PrimaryScreenWidth and SystemParameters.PrimaryScreenHeight
  • SystemEvents: this class contains a lot of useful events that allow you to be notifyed about useful system changes.  In the specific case the event SystemEvents.DisplaySettingsChanged is raised every time the screen resolution chage.

The following example code illustrates how to use them:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

ShowScreenResolution();

SystemEvents.DisplaySettingsChanged +=
new EventHandler(SystemEvents_DisplaySettingsChanged);
}

void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
this.ShowScreenResolution();
}

private void ShowScreenResolution()
{
MessageBox.Show(String.Format("Screen Resolution: {0}x{1}",
SystemParameters.PrimaryScreenWidth,
SystemParameters.PrimaryScreenHeight));

}
}
Advertisements