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

摘要:
让我们考虑一下。下面是使用Enum Display Devices()API调用来枚举系统上的显示设备,并查找已完成Display_DEVICE_ATTACHED_to_DESKTOP标记集的显示设备(这将包括所有错误设备

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

免责声明:文章转载自《关于将桌面扩展到监视器的问题 extended my windows desktop onto this monitor》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle 性能调优 概述SQL语句Where中使用别名作为判断条件下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

AOP+自定义注解实现全局参数校验

AOP+自定义注解实现全局参数校验 在开发过程中,用户传递的数据不一定合法,虽然可以通过前端进行一些校验,但是为了确保程序的安全性,保证数据的合法,在后台进行数据校验也是十分必要的。 后台的参数校验 在controller方法中校验: 后台的参数是通过controller方法获取的,所以最简单的参数校验的方法,就是在controller方法中进行参数校验。...

C# 通过ServiceStack 操作Redis——Hash类型的使用及示例

接着上一篇,下面转到hash类型的代码使用 Hash:结构 key-key-value,通过索引快速定位到指定元素的,可直接修改某个字段 /// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 /// str...

http协议的POST传数据

PostRequest使用StreamWriter对象写入请求流,不需要使用HttpUtility.UrlEncode显示转码,而下面的需要显示转码,还需要将参数转为字节码 蛋疼…………。 public static string PostRequest(string url, string postData) { HttpWebRequest httpW...

[go]gin框架

gin中文文档gin example repo gin参考 gin框架基础使用-梳理版 - 最简单的gin启服务 package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() r.Run() //端口默认8080 } RESPONSE RENDER...

js和java中使用join来进行数组元素的连接

  合理地使用join来进行数组中元素的连接,代码简洁,比自己编码也更为方便。   js中join的用法 var arr = new Array(3) arr[0] = "a" arr[1] = "b" arr[2] = "c" arr.join(".") a.b.c ------------------ arr.join(",") a,b,c   ja...

关于Java配置文件properties的学习

关于Java配置文件properties的学习摘自:https://www.cnblogs.com/Seanit/p/4555937.html 在Java早期的开发中,常用*.properties文件存储一些配置信息。其文件中的信息主要是以key=value的方式进行存储,在早期受到广泛的应用。而后随着xml使用的广泛,其位置渐渐被取代,不过,目前仍有一...