Get system metrics in C#

[system metrics]

The GetSystemMetrics API function returns system metrics, values that give dimensions used by the system such as the default size of icons and the thickness of a resizable window’s borders. For example, the highlighted line in the picture on the right shows that on my system the default icons width (SM_CXICON) is 32 pixels.

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 system metrics values 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 system 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 system metrics.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in API, system and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Get system metrics in C#

  1. Pingback: Display system metric values with descriptions in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *