Get system metrics in C#

The GetSystemMetrics API function returns values that give dimensions used by the system such as the default size of icons and the thickness of a resizable window's borders.

The following code shows how the program declares the GetSystemMetrics API function. (Note that the program includes the System.Runtime.InteropServices namespace so it can use DllImport.)

[DllImport("user32.dll")]
static extern int GetSystemMetrics(SystemMetric smIndex);

The following SystemMetric enumeration defines the parameters that you can pass to the GetSystemMetrics function.

public enum SystemMetric
{
    SM_CXSCREEN = 0,  // 0x00
    SM_CYSCREEN = 1,  // 0x01
    SM_CXVSCROLL = 2,  // 0x02
    SM_CYHSCROLL = 3,  // 0x03
    ...
    SM_REMOTECONTROL = 0x2001, // 0x2001
}

The form's Load event handler uses the following code to display the metric values.

// Display some useful metrics.
private void Form1_Load(object sender, EventArgs e)
{
    AddValue(SystemMetric.SM_CXSCREEN);
    AddValue(SystemMetric.SM_CYSCREEN);
    ...
}

The following AddValue method displays a metric's name and value in the lvwMetrics ListView control.

// Add a value to the ListView.
private void AddValue(SystemMetric metric)
{
    ListViewItem item = lvwMetrics.Items.Add(metric.ToString());
    item.SubItems.Add(GetSystemMetrics(metric).ToString());
}

Download the example and look at the code to see all 89 of the metrics.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.