關於將案頭擴充到監視器的問題 extended my windows desktop onto this monitor

來源:互聯網
上載者:User

說下思路吧 下面是網上找的
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.