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

EnumDisplayDevices (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll", CharSet=CharSet.Unicode))]
static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum,
   ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);

User-Defined Types:

DISPLAY_DEVICE

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

  // This method will return the size of the complete desktop area, taking all attached devices into account.
  public Size GetDesktopSize()
  {
    Rectangle desktop = new Rectangle();

    DISPLAY_DEVICE device = new DISPLAY_DEVICE();
    device.cb = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DISPLAY_DEVICE));
    device.DeviceName = new string(' ', 32);
    device.DeviceString = new string(' ', 128);
    device.DeviceKey = new string(' ', 128);
    device.DeviceID = new string(' ', 128);

    for(uint i = 0; ; i++)
    {
      if (!Win32.EnumDisplayDevices(null, i, ref device, 0))
    break;

      // If it's not connected to the desktop, then ignore it.
      if ((device.StateFlags & 1) == 0)
    continue;

      // Get the settings of the current display mode for this device.
      DEVMODE devMode = new DEVMODE();
      if (!Win32.EnumDisplaySettings(device.DeviceName, uint.MaxValue, ref devMode))
    continue;

      // This is the rectangle defined by this device
      Rectangle deviceRect = new Rectangle(devMode.dmPosition.x, devMode.dmPosition.y,
             devMode.dmPelsWidth, devMode.dmPelsHeight);

      // Update the "desktop" rectangle with the new device
      if (desktop.Left > deviceRect.Left)
    desktop.Location = new Point(deviceRect.Left, desktop.Y);
      if (desktop.Top > deviceRect.Top)
    desktop.Location = new Point(desktop.X, deviceRect.Top);
      if (desktop.Right < deviceRect.Right)
    desktop.Size = new Size(deviceRect.Width, desktop.Height);
      if (desktop.Bottom < deviceRect.Bottom)
    desktop.Size = new Size(desktop.Width, deviceRect.Height);
    }

    // And return the size of the total desktop.
    return desktop.Size;
  }

Alternative Managed API:

Do you know one? Please contribute it!

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
Find References
Show Printable Version
Revisions