Answered Multiple Display Configuration's setting change in win 8

  • Thursday, June 07, 2012 1:37 AM
     
     

    In win8, it has Multiple Display Configuration in "Second screen".
    PC screen only, Duplicate, Extend and Second screen only.
    My question is how to get the status changed event by WndProc messages : HwndSource in c#?

    Does anyone have similar experiences?

All Replies

  • Friday, June 08, 2012 5:26 AM
    Moderator
     
      Has Code

    Same with earlier OS, we could p/invoke the EnumDisplaySettings API to get the DEVMODE, and

    • in  Extend mode, the two or multiple screens dmPosition value are different, for example, my two monitors are set as, the values are: (0,0) and (1366,-512)

    Code:

      [DllImport("user32.dll")]
      public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
    
      public struct POINTL
      {
          public Int32 x;
          public Int32 y;
      }
    
      [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
      public struct DEVMODE
      {
          public const int CCHDEVICENAME = 32;
          public const int CCHFORMNAME = 32;
    
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
          [System.Runtime.InteropServices.FieldOffset(0)]
          public string dmDeviceName;
          [System.Runtime.InteropServices.FieldOffset(32)]
          public Int16 dmSpecVersion;
          [System.Runtime.InteropServices.FieldOffset(34)]
          public Int16 dmDriverVersion;
          [System.Runtime.InteropServices.FieldOffset(36)]
          public Int16 dmSize;
          [System.Runtime.InteropServices.FieldOffset(38)]
          public Int16 dmDriverExtra;
          [System.Runtime.InteropServices.FieldOffset(40)]
          public DM dmFields;
    
          [System.Runtime.InteropServices.FieldOffset(44)]
          Int16 dmOrientation;
          [System.Runtime.InteropServices.FieldOffset(46)]
          Int16 dmPaperSize;
          [System.Runtime.InteropServices.FieldOffset(48)]
          Int16 dmPaperLength;
          [System.Runtime.InteropServices.FieldOffset(50)]
          Int16 dmPaperWidth;
          [System.Runtime.InteropServices.FieldOffset(52)]
          Int16 dmScale;
          [System.Runtime.InteropServices.FieldOffset(54)]
          Int16 dmCopies;
          [System.Runtime.InteropServices.FieldOffset(56)]
          Int16 dmDefaultSource;
          [System.Runtime.InteropServices.FieldOffset(58)]
          Int16 dmPrintQuality;
    
          [System.Runtime.InteropServices.FieldOffset(44)]
          public POINTL dmPosition;
          [System.Runtime.InteropServices.FieldOffset(52)]
          public Int32 dmDisplayOrientation;
          [System.Runtime.InteropServices.FieldOffset(56)]
          public Int32 dmDisplayFixedOutput;
    
          [System.Runtime.InteropServices.FieldOffset(60)]
          public short dmColor;
          [System.Runtime.InteropServices.FieldOffset(62)]
          public short dmDuplex;
          [System.Runtime.InteropServices.FieldOffset(64)]
          public short dmYResolution;
          [System.Runtime.InteropServices.FieldOffset(66)]
          public short dmTTOption;
          [System.Runtime.InteropServices.FieldOffset(68)]
          public short dmCollate;
          [System.Runtime.InteropServices.FieldOffset(72)]
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
          public string dmFormName;
          [System.Runtime.InteropServices.FieldOffset(102)]
          public Int16 dmLogPixels;
          [System.Runtime.InteropServices.FieldOffset(104)]
          public Int32 dmBitsPerPel;
          [System.Runtime.InteropServices.FieldOffset(108)]
          public Int32 dmPelsWidth;
          [System.Runtime.InteropServices.FieldOffset(112)]
          public Int32 dmPelsHeight;
          [System.Runtime.InteropServices.FieldOffset(116)]
          public Int32 dmDisplayFlags;
          [System.Runtime.InteropServices.FieldOffset(116)]
          public Int32 dmNup;
          [System.Runtime.InteropServices.FieldOffset(120)]
          public Int32 dmDisplayFrequency;
      }
      [Flags()]
      public enum DM : int
      {
          Orientation = 0x1,
          PaperSize = 0x2,
          PaperLength = 0x4,
          PaperWidth = 0x8,
          Scale = 0x10,
          Position = 0x20,
          NUP = 0x40,
          DisplayOrientation = 0x80,
          Copies = 0x100,
          DefaultSource = 0x200,
          PrintQuality = 0x400,
          Color = 0x800,
          Duplex = 0x1000,
          YResolution = 0x2000,
          TTOption = 0x4000,
          Collate = 0x8000,
          FormName = 0x10000,
          LogPixels = 0x20000,
          BitsPerPixel = 0x40000,
          PelsWidth = 0x80000,
          PelsHeight = 0x100000,
          DisplayFlags = 0x200000,
          DisplayFrequency = 0x400000,
          ICMMethod = 0x800000,
          ICMIntent = 0x1000000,
          MediaType = 0x2000000,
          DitherType = 0x4000000,
          PanningWidth = 0x8000000,
          PanningHeight = 0x10000000,
          DisplayFixedOutput = 0x20000000
      }
      public const int ENUM_CURRENT_SETTINGS = -1;
      
      ......
      foreach (Screen screen in Screen.AllScreens)
      {
    
          DEVMODE dm = new DEVMODE();
          dm.dmDeviceName = new String(new char[32]);
          dm.dmFormName = new String(new char[32]);
          dm.dmSize = (short)Marshal.SizeOf(dm);
    
          EnumDisplaySettings(screen.DeviceName, ENUM_CURRENT_SETTINGS, ref dm);
          POINTL position = (POINTL)dm.dmPosition;
          Console.WriteLine("{0}, position is : ({1},{2})", screen.DeviceName, position.x, position.y);
      }        
      ......

    • if it is show only in one screen or duplicate, there is only one Screen instance in the Screen.AllScreens

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, June 11, 2012 2:51 AM
     
     Answered

    Thanks for Bob's reply.

    I think I found the answer.
    WM_DISPLAYCHANGE : 0x7e
    http://msdn.microsoft.com/en-us/library/windows/desktop/dd145210(v=vs.85).aspx


    • Marked As Answer by hatasum Monday, June 11, 2012 2:51 AM
    • Edited by hatasum Monday, June 11, 2012 3:31 AM
    •