[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Get system metrics in C#

[Get system metrics in C#]

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 to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.