Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

getsystemmetrics (user32)
 
.
Summary
Retrieves various system metrics of display elements and system configuration settings.

C# Signature:

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

VB.NET Signature:

Public Declare Auto Function GetSystemMetrics Lib "user32.dll" (ByVal smIndex As Integer) As Integer

Parameters:

SystemMetric smIndex
System metric or configuration setting to retrieve. This parameter can be one of the following values. Note that all SM_CX* values are widths and all SM_CY* values are heights. Also note that all settings designed to return Boolean data represent TRUE as any nonzero value, and FALSE as a zero value.

User-Defined Types:

The C# and VB Definition of the SystemMetric enum can be found here:

SystemMetric http://pinvoke.net/default.aspx/Enums.SystemMetric

Remarks:

* System metrics may vary from display to display. Most values are returned in number of pixels, all others are flag or boolean values.

* GetSystemMetrics(SM_CMONITORS) counts only display monitors. This is different from EnumDisplayMonitors, which enumerates display monitors and also non-display pseudo-monitors.

The SM_ARRANGE setting specifies how the system arranges minimized windows, and consists of a starting position and a direction. The starting position can be one of the following values.

Value Meaning
ARW_BOTTOMLEFT Start at the lower-left corner of the screen (default position).
ARW_BOTTOMRIGHT Start at the lower-right corner of the screen. Equivalent to ARW_STARTRIGHT.
ARW_HIDE Hide minimized windows by moving them off the visible area of the screen.
ARW_TOPLEFT Start at the upper-left corner of the screen. Equivalent to ARV_STARTTOP.
ARW_TOPRIGHT Start at the upper-right corner of the screen. Equivalent to ARW_STARTTOP | SRW_STARTRIGHT.

The direction in which to arrange can be one of the following values.

Value Meaning
ARW_DOWN Arrange vertically, top to bottom.
ARW_LEFT Arrange horizontally, left to right.
ARW_RIGHT Arrange horizontally, right to left.
ARW_UP Arrange vertically, bottom to top.

Tips & Tricks:

Use the Form.ClientSize method to get the size of the working area of the window, which is much easier than calculating the system

metrics yourself to get the same result (something that you could do if you had to, and had to do back in the day):

Sample Code C#:

The following example creates a new ListView control, and displays each member of the enumerator and it's return value. To use this example, paste it into the code (not the deigner code) file of a blank form. (ie, Form1.cs), if you have AIPCC installed you wont need to use the WindowsAPI class, instead it will be imported fron the AIP class library.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CodeCoreUI_Diagnostics
{
     public partial class Form1:Form
     {
     public partial class WindowsAPI
     {
  //         *** PASTE SYSTEMMETRIC ENUM HERE ***
          [DllImport("user32.dll")]
          public static extern int GetSystemMetrics(SystemMetric smIndex);
      }

     public Form1()
     {
         InitializeComponent();
     }

     private void Form1_Load(object sender,EventArgs e)
     {        
         ListView lvMain = new ListView();
         this.Controls.Add(lvMain);

         lvMain.Visible=true;
         lvMain.Location=new Point(0,0);
         lvMain.Dock = DockStyle.Fill;
         lvMain.Columns.Add("Name",200);
         lvMain.Columns.Add("Value",200) ;
         lvMain.Columns.Add("Returned",200);

         lvMain.View=View.Details;


         ListViewItem lvi;
         string x;
         int u;

         foreach (int i in Enum.GetValues(typeof(WindowsAPI.SystemMetric)))
         {
         x = Enum.GetName(typeof(WindowsAPI.SystemMetric),i);
         u=WindowsAPI.GetSystemMetrics(( WindowsAPI.SystemMetric)i);
         lvi = lvMain.Items.Add(x);        
         lvi.SubItems.Add(i.ToString());
         lvi.SubItems.Add(u.ToString());
         }
     }
     }
}

Alternative Managed API:

System.Windows.Forms.SystemInformation class

AIPCC.NET Windows API Library is available for free use (still in development)

The extensions are semi-managed via the GC and Exception Handling.

Site Resources:

Documentation tested on Microsoft Visual Studio 2005

Information Supplied or Quoted from Microsoft Developer News (MSDN) 2006 Feb.

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions