关于将桌面扩展到监视器的问题 extended my windows desktop onto this monitor

说下思路吧 下面是网上找的
Use the EnumDisplayDevices() API call to enumerate the display devices on the system and look for those that don't have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP flag set (this will include any mirroring devices so not all will be physical displays.) Once you've found the display device you'll need to get a valid display mode to change it to, you can find this by calling the EnumDisplaySettingsEx() API call - Generally you'd display all the available modes and allow the user to choose however in your case it sounds like this may be possible to hard-code and save you an additional step. For the sake of future-proofing your application though I'd suggest having this easily changeable without having to dig through the source every time, a registry key would be the obvious choice. Once you've got that sorted out populate a DevMode display structure with the information about the display positioning (set the PelsWidth/Height, Position, DisplayFrequency and BitsPerPel properties) then set these flags in the fields member. Finally call ChangeDisplaySettingsEx() with this settings structure and be sure to send the reset and update registry flags. That should be all you need, hope this helps,

上面的意思就不解释了

解决步骤

DISPLAY_DEVICE structure import using PInvoke 这个就是显示器的一些信息

EnumDisplayDevices function import 能够通过显示器的索引的得到 DISPLAY_DEVICE

EnumDisplaySettingsEx function import 设置 显示器的信息

ChangeDisplaySettingsEx 把桌面扩展到设置好的display上面

#region code

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsSreen
{
    class MulScreen
    {
        public const int DM_ORIENTATION = 0x00000001;

        public const int DM_PAPERSIZE = 0x00000002;

        public const int DM_PAPERLENGTH = 0x00000004;

        public const int DM_PAPERWIDTH = 0x00000008;

        public const int DM_SCALE = 0x00000010;

        public const int DM_POSITION = 0x00000020;

        public const int DM_NUP = 0x00000040;

        public const int DM_DISPLAYORIENTATION = 0x00000080;

        public const int DM_COPIES = 0x00000100;

        public const int DM_DEFAULTSOURCE = 0x00000200;

        public const int DM_PRINTQUALITY = 0x00000400;

        public const int DM_COLOR = 0x00000800;

        public const int DM_DUPLEX = 0x00001000;

        public const int DM_YRESOLUTION = 0x00002000;

        public const int DM_TTOPTION = 0x00004000;

        public const int DM_COLLATE = 0x00008000;

        public const int DM_FORMNAME = 0x00010000;

        public const int DM_LOGPIXELS = 0x00020000;

        public const int DM_BITSPERPEL = 0x00040000;

        public const int DM_PELSWIDTH = 0x00080000;

        public const int DM_PELSHEIGHT = 0x00100000;

        public const int DM_DISPLAYFLAGS = 0x00200000;

        public const int DM_DISPLAYFREQUENCY = 0x00400000;

        public const int DM_ICMMETHOD = 0x00800000;

        public const int DM_ICMINTENT = 0x01000000;

        public const int DM_MEDIATYPE = 0x02000000;

        public const int DM_DITHERTYPE = 0x04000000;

        public const int DM_PANNINGWIDTH = 0x08000000;

        public const int DM_PANNINGHEIGHT = 0x10000000;

        public const int DM_DISPLAYFIXEDOUTPUT = 0x20000000;

       const uint EWX_FORCE = 4;
       const uint DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 1;
       [DllImport("user32.dll")]
       static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum,
          ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
      
       [DllImport("user32.dll")]
       static extern int ChangeDisplaySettingsEx(string lpszDeviceName,
          ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam);
       [DllImport("user32.dll")]
       public static extern int EnumDisplaySettings(
             string deviceName, int modeNum, ref DEVMODE devMode);
       public struct DISPLAY_DEVICE
       {
           [MarshalAs(UnmanagedType.U4)]
           public int cb;

           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
           public string DeviceName;

           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
           public string DeviceString;

           [MarshalAs(UnmanagedType.U4)]
           public uint StateFlags;

           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
           public string DeviceID;

           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
           public string DeviceKey;
       }
       [StructLayout(LayoutKind.Sequential)]
       public struct DEVMODE
       {
           public const int CCHDEVICENAME = 32;
           public const int CCHFORMNAME = 32;

           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
           public string dmDeviceName;
           public short dmSpecVersion;
           public short dmDriverVersion;
           public short dmSize;
           public short dmDriverExtra;
           public int dmFields;

           public short dmOrientation;
           public short dmPaperSize;
           public short dmPaperLength;
           public short dmPaperWidth;

           public short dmScale;
           public short dmCopies;
           public short dmDefaultSource;
           public short dmPrintQuality;
           public short dmColor;
           public short dmDuplex;
           public short dmYResolution;
           public short dmTTOption;
           public short dmCollate;
           [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
           public string dmFormName;
           public short dmLogPixels;
           public int dmBitsPerPel;    // Declared wrong in the full framework
           public int dmPelsWidth;
           public int dmPelsHeight;
           public int dmDisplayFlags;
           public int dmDisplayFrequency;

           public int dmICMMethod;
           public int dmICMIntent;
           public int dmMediaType;
           public int dmDitherType;
           public int dmReserved1;
           public int dmReserved2;
           public int dmPanningWidth;
           public int dmPanningHeight;

           public int dmPositionX; // Using a PointL Struct does not work
           public int dmPositionY;


       }


       public  void Mul()
       {
           DISPLAY_DEVICE d = new DISPLAY_DEVICE();
           DEVMODE dm = new DEVMODE();
           d.cb = Marshal.SizeOf(d);
           int deviceID = 1; // This is only for my device setting. Go through the whole EnumDisplayDevices to find your device 
           EnumDisplayDevices(null, (uint)deviceID, ref  d, 0); //
           EnumDisplaySettings(d.DeviceName, 0, ref dm);
           dm.dmPelsWidth = 1024;
           dm.dmPelsHeight = 768;
           dm.dmPositionX = Screen.PrimaryScreen.Bounds.Right;
           dm.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT;
           ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, DISPLAY_DEVICE_ATTACHED_TO_DESKTOP, IntPtr.Zero);

         
       }

    }
}

#endregon

微软BI技术交流群:316744959 武汉NET技术群:961108969 NET技术群:21386099 本人具有丰富的系统开发经验,承接系统开发,小程序,NET系统开发,BI开发,有需求联系微信手机:15010195887
原文地址:https://www.cnblogs.com/Impulse/p/1433007.html