c#-C# 请教这个设置多显示器复制扩展的类怎么调用?麻烦各位了!

问题描述

C# 请教这个设置多显示器复制扩展的类怎么调用?麻烦各位了!
 #region Copyright (C) 2005-2011 Team MediaPortal// Copyright (C) 2005-2011 Team MediaPortal// http://www.team-mediaportal.com// // MediaPortal is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation either version 2 of the License or// (at your option) any later version.// // MediaPortal is distributed in the hope that it will be useful// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with MediaPortal. If not see <http://www.gnu.org/licenses/>.#endregion#region usingusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;#endregionnamespace ImageViewer{    public class W7RefreshRateHelper    {        #region consts        private const uint SIZE_OF_DISPLAYCONFIG_PATH_INFO = 72;        private const uint SIZE_OF_DISPLAYCONFIG_MODE_INFO = 64;        private const uint QDC_ALL_PATHS = 1;        private const uint DISPLAYCONFIG_MODE_INFO_TYPE_TARGET = 2;        private const uint DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE = 1;        public const uint SDC_TOPOLOGY_INTERNAL = 0x00000001;        public const uint SDC_TOPOLOGY_CLONE = 0x00000002;        public const uint SDC_TOPOLOGY_EXTEND = 0x00000004;        public const uint SDC_TOPOLOGY_EXTERNAL = 0x00000008;        public const uint SDC_TOPOLOGY_SUPPLIED = 0x00000010;        public const uint SDC_USE_DATABASE_CURRENT = (SDC_TOPOLOGY_INTERNAL | SDC_TOPOLOGY_CLONE | SDC_TOPOLOGY_EXTEND | SDC_TOPOLOGY_EXTERNAL);        private const uint SDC_USE_SUPPLIED_DISPLAY_CONFIG = 0x00000020;        private const uint SDC_VALIDATE = 0x00000040;        private const uint SDC_APPLY = 0x00000080;        private const uint SDC_ALLOW_CHANGES = 0x00000400;        private const uint SDC_NO_OPTIMIZATION = 0x00000100;        private const uint SDC_SAVE_TO_DATABASE = 0x00000200;        private const uint SDC_PATH_PERSIST_IF_REQUIRED = 0x00000800;        private const uint SDC_FORCE_MODE_ENUMERATION = 0x00001000;        private const uint SDC_ALLOW_PATH_ORDER_CHANGES = 0x00002000;        #endregion        #region DLL imports        [DllImport(""user32.dll"" CharSet = CharSet.Unicode)]        private static extern long GetDisplayConfigBufferSizes([In] uint flags [Out] out uint numPathArrayElements                                                               [Out] out uint numModeArrayElements);        [DllImport(""user32.dll"" CharSet = CharSet.Unicode)]        private static extern long QueryDisplayConfig([In] uint flags ref uint numPathArrayElements IntPtr pathArray                                                      ref uint numModeArrayElements IntPtr modeArray                                                      IntPtr currentTopologyId);        [DllImport(""user32.dll"" CharSet = CharSet.Unicode)]        private static extern long SetDisplayConfig(uint numPathArrayElements IntPtr pathArray uint numModeArrayElements                                                    IntPtr modeArray uint flags);        #endregion        #region private members        private static int GetModeInfoOffsetForDisplayId(uint displayIndex IntPtr pModeArray uint uNumModeArrayElements)        {            int offset;            int modeInfoType;            // there are always two mode infos per display (target and source)            offset = (int)(displayIndex * SIZE_OF_DISPLAYCONFIG_MODE_INFO * 2);            // out of bounds sanity check            if (offset + SIZE_OF_DISPLAYCONFIG_MODE_INFO >= uNumModeArrayElements * SIZE_OF_DISPLAYCONFIG_MODE_INFO)            {                return -1;            }            // check which one of the two mode infos for the display is the target            modeInfoType = Marshal.ReadInt32(pModeArray offset);            if (modeInfoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)            {                return offset;            }            else            {                offset += (int)SIZE_OF_DISPLAYCONFIG_MODE_INFO;            }            modeInfoType = Marshal.ReadInt32(pModeArray offset);            if (modeInfoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)            {                return offset;            }            // no target mode info found this should never happen            else            {                return -1;            }        }        #endregion        #region public members        public static double GetRefreshRate(uint displayIndex)        {            uint uNumPathArrayElements = 0;            uint uNumModeArrayElements = 0;            IntPtr pPathArray = IntPtr.Zero;            IntPtr pModeArray = IntPtr.Zero;            IntPtr pCurrentTopologyId = IntPtr.Zero;            long result;            UInt32 numerator;            UInt32 denominator;            double refreshRate;            // get size of buffers for QueryDisplayConfig            result = GetDisplayConfigBufferSizes(QDC_ALL_PATHS out uNumPathArrayElements out uNumModeArrayElements);            if (result != 0)            {                //   Log.Error(""W7RefreshRateHelper.GetRefreshRate: GetDisplayConfigBufferSizes(...) returned {0}"" result);                return 0;            }            // allocate memory or QueryDisplayConfig buffers            pPathArray = Marshal.AllocHGlobal((Int32)(uNumPathArrayElements * SIZE_OF_DISPLAYCONFIG_PATH_INFO));            pModeArray = Marshal.AllocHGlobal((Int32)(uNumModeArrayElements * SIZE_OF_DISPLAYCONFIG_MODE_INFO));            // get display configuration            result = QueryDisplayConfig(QDC_ALL_PATHS                                        ref uNumPathArrayElements pPathArray                                        ref uNumModeArrayElements pModeArray                                        pCurrentTopologyId);            // if failed log error message and free memory            if (result != 0)            {                //   Log.Error(""W7RefreshRateHelper.GetRefreshRate: QueryDisplayConfig(...) returned {0}"" result);                Marshal.FreeHGlobal(pPathArray);                Marshal.FreeHGlobal(pModeArray);                return 0;            }            // get offset for a display's target mode info            int offset = GetModeInfoOffsetForDisplayId(displayIndex pModeArray uNumModeArrayElements);            // if failed log error message and free memory            if (offset == -1)            {                //Log.Error(""W7RefreshRateHelper.GetRefreshRate: Couldn't find a target mode info for display {0}"" displayIndex);                Marshal.FreeHGlobal(pPathArray);                Marshal.FreeHGlobal(pModeArray);                return 0;            }            // get refresh rate            numerator = (UInt32)Marshal.ReadInt32(pModeArray offset + 32);            denominator = (UInt32)Marshal.ReadInt32(pModeArray offset + 36);            refreshRate = (double)numerator / (double)denominator;            //Log.Debug(""W7RefreshRateHelper.GetRefreshRate: QueryDisplayConfig returned {0}/{1}"" numerator denominator);            // free memory and return refresh rate            Marshal.FreeHGlobal(pPathArray);            Marshal.FreeHGlobal(pModeArray);            return refreshRate;        }        public static bool SetRefreshRate(uint displayIndex double refreshRate)        {            uint uNumPathArrayElements = 0;            uint uNumModeArrayElements = 0;            IntPtr pPathArray = IntPtr.Zero;            IntPtr pModeArray = IntPtr.Zero;            IntPtr pCurrentTopologyId = IntPtr.Zero;            long result;            UInt32 numerator;            UInt32 denominator;            UInt32 scanLineOrdering;            UInt32 flags;            // get size of buffers for QueryDisplayConfig            result = GetDisplayConfigBufferSizes(QDC_ALL_PATHS out uNumPathArrayElements out uNumModeArrayElements);            if (result != 0)            {                // Log.Error(""W7RefreshRateHelper.GetRefreshRate: GetDisplayConfigBufferSizes(...) returned {0}"" result);                return false;            }            // allocate memory or QueryDisplayConfig buffers            pPathArray = Marshal.AllocHGlobal((Int32)(uNumPathArrayElements * SIZE_OF_DISPLAYCONFIG_PATH_INFO));            pModeArray = Marshal.AllocHGlobal((Int32)(uNumModeArrayElements * SIZE_OF_DISPLAYCONFIG_MODE_INFO));            // get display configuration            result = QueryDisplayConfig(QDC_ALL_PATHS                                        ref uNumPathArrayElements pPathArray                                        ref uNumModeArrayElements pModeArray                                        pCurrentTopologyId);            // if failed log error message and free memory            if (result != 0)            {                //   Log.Error(""W7RefreshRateHelper.GetRefreshRate: QueryDisplayConfig(...) returned {0}"" result);                Marshal.FreeHGlobal(pPathArray);                Marshal.FreeHGlobal(pModeArray);                return false;            }            // get offset for a display's target mode info            int offset = GetModeInfoOffsetForDisplayId(displayIndex pModeArray uNumModeArrayElements);            // if failed log error message and free memory            if (offset == -1)            {                //Log.Error(""W7RefreshRateHelper.GetRefreshRate: Couldn't find a target mode info for display {0}"" displayIndex);                Marshal.FreeHGlobal(pPathArray);                Marshal.FreeHGlobal(pModeArray);                return false;            }            // TODO: refactor to private method            // set proper numerator and denominator for refresh rate            UInt32 newRefreshRate = (uint)(refreshRate * 1000);            switch (newRefreshRate)            {                case 23976:                    numerator = 24000;                    denominator = 1001;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 24000:                    numerator = 24000;                    denominator = 1000;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 25000:                    numerator = 25000;                    denominator = 1000;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 30000:                    numerator = 30000;                    denominator = 1000;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 50000:                    numerator = 50000;                    denominator = 1000;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 59940:                    numerator = 60000;                    denominator = 1001;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                case 60000:                    numerator = 60000;                    denominator = 1000;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;                default:                    numerator = (uint)(newRefreshRate / 1000);                    denominator = 1;                    scanLineOrdering = DISPLAYCONFIG_SCANLINE_ORDERING_PROGRESSIVE;                    break;            }            // set refresh rate parameters in display config            Marshal.WriteInt32(pModeArray offset + 32 (int)numerator);            Marshal.WriteInt32(pModeArray offset + 36 (int)denominator);            Marshal.WriteInt32(pModeArray offset + 56 (int)scanLineOrdering);            // validate new refresh rate            flags = SDC_VALIDATE | SDC_USE_SUPPLIED_DISPLAY_CONFIG;            result = SetDisplayConfig(uNumPathArrayElements pPathArray uNumModeArrayElements pModeArray flags);            // adding SDC_ALLOW_CHANGES to flags if validation failed            if (result != 0)            {                //Log.Debug(""W7RefreshRateHelper.SetDisplayConfig(...): SDC_VALIDATE of {0}/{1} failed"" numerator denominator);                flags = SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_ALLOW_CHANGES;            }            else            {                //Log.Debug(""W7RefreshRateHelper.SetDisplayConfig(...): SDC_VALIDATE of {0}/{1} succesful"" numerator denominator);                flags = SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG;            }            // configuring display            result = SetDisplayConfig(uNumPathArrayElements pPathArray uNumModeArrayElements pModeArray flags);            // if failed log error message and free memory            if (result != 0)            {                //Log.Error(""W7RefreshRateHelper.SetDisplayConfig(...): SDC_APPLY returned {0}"" result);                Marshal.FreeHGlobal(pPathArray);                Marshal.FreeHGlobal(pModeArray);                return false;            }            // refresh rate change successful               Marshal.FreeHGlobal(pPathArray);            Marshal.FreeHGlobal(pModeArray);            return true;        }        public static bool SetScreen(uint displayModel)        {            return SetDisplayConfig(0 IntPtr.Zero 0 IntPtr.Zero SDC_APPLY | displayModel) == 0;        }        #endregion    }}

解决方案

因为这个类里面的方法都是静态的,所以你可以直接使用类似ImageViewer.W7RefreshRateHelper.SetScreen(1)的方式调用,不需要实例化。

解决方案二:
怎么调用看什么需求
通用的调用无非是实例化类 然后用类名.方法名
由于方法是静态方法 可以不用实例化 直接调用

时间: 2024-10-06 11:14:12

c#-C# 请教这个设置多显示器复制扩展的类怎么调用?麻烦各位了!的相关文章

Win7如何设置双显示器?

  一般情况我们的电脑都是一台电脑只配一个显示器,但因工作的需要,会用到两台,甚至三台以上的显示器.使用Win7系统用户,想设置一台电脑两个显示器.要如何设置?设置双显示器有什么方法?下面就和大家说一下Win7系统设置双显示器的方法. 步骤如下: 一.三种不同的设置: "扩展显示": 将两台显示器都连接上电脑后,默认情况下是"扩展显示"的功能.所谓扩展显示,就是将显示画面扩大到两个显示器的范围,例如我的机器是一台 1440X900分辨率与一台1280X1024分辨率

怎么设置双显示器

  三种不同的设置 1"扩展显示" 将两台显示器都连接上电脑后,默认情况下是"扩展显示"的功能.所谓扩展显示,就是将显示画面扩大到两个显示器的范围,例如我的机器是一台 1440X900分辨率与一台1280X1024分辨率的机器,扩展显示后 总分辨率就是 1440X900+1280X1024. 显示效果请参阅图二.图三,此时有且仅有一台显示器具有任务栏(我的任务栏置于顶部) 如果需要将任务栏置于另外一台显示器,请参阅图四 2"复制显示" 如果采用

请教一下,我怎么复制一下table的结构给一个新的表。我不要复制数据。

问题描述 请教一下,我怎么复制一下table的结构给一个新的表.我不要复制数据. 解决方案 解决方案二:clone()解决方案三:没说清楚呀,是数据库里的物理表还是内存里的逻辑表?解决方案四:数据库里的表就读到datatable或dataset中,然后clone()解决方案五:就是Clone(),专门复制表达结构.解决方案六:引用1楼zhoukang0916的回复: clone() 没错,克隆解决方案七:克隆只是浅复制吧?而楼主的意思是要深克隆.就是另一个表已经与被复制的那张表所持有的对数据的引

如何编程实现对投影仪连接方式的设置(如复制或扩展)

问题描述 如何编程实现对投影仪连接方式的设置(如复制屏幕或扩展屏幕) 解决方案 解决方案二:目前我知道的是,用模拟输入来控制.你找找windows7的api看看能不能找着!解决方案三:查到有个:SetDisplayMode()不知如何用

请教 自己写的mysqli 操作数据库的类 DB.class.php

问题描述 请教 自己写的mysqli 操作数据库的类 DB.class.php 类是这样写的: <?php class DB{ //属性 private $host; private $port; private $name; private $pass; private $dbname; private $prefix; //设置表前缀 private $charset;//设置字符集 private $mysqli; //设置mysqli类对象 //设置构造函数 public functio

Android开发 设置系统字体,重启activity,多调用一次onPause

问题描述 Android开发 设置系统字体,重启activity,多调用一次onPause android 当前应用Home键,然后设置系统语言,在进入应用时重启Activity但是,执行到onResume后自动又执行了onPause,请问哪位大神知道为什么 解决方案 你倒是把源码贴出来给我看看呀 解决方案二: activity的启动模式有没有问题?

怎么设置双显示器显示

首先,检验笔记本是否具有双屏显示(DualView)功能. 打开"显示属性"对话框的"设置"项,如果出现有两个显示器图标,表明显卡支持双屏显示(DualView):如果没有两个显示器图标出现,只看到桌面的缩小图标,表示显卡不支持双屏显示(DualView). 其次,将笔记本电脑扩展成双屏显示的设置如下: 将外接监视器连接在笔记本的VGA端口上,打开笔记本的"显示属性"对话框,选择"设置"选项,可以看到两个监视器的图标: 点击&

电脑怎么设置自动关闭显示器的时间?

  当我们长时间不在电脑跟前的时候,习惯将电脑屏幕切换到锁屏模式,一方面能起到节能环保的作用,另一方面又可以保护自己电脑里的隐私.然后当我们忘记锁屏的时候,如何才能设置电脑自动关闭显示器的时间呢? 1.首先在电脑上打开[控制面板]选项,可通过左下角的开始菜单进入控制面板选项,在控制面板界面可以看到[个性化]选项,如下图所示. 2.点击[个性化]选项,进入控制面板个性化界面,在左侧菜单栏里可以看到[显示]选项,如下图所示. 3.点击[显示]选项,进入控制面板显示界面,在左侧菜单栏里可以找的[调整亮

电脑如何设置自动关闭显示器的时间

  1.首先在电脑上打开[控制面板]选项,可通过左下角的开始菜单进入控制面板选项,在控制面板界面可以看到[个性化]选项,如下图所示. 2.点击[个性化]选项,进入控制面板个性化界面,在左侧菜单栏里可以看到[显示]选项,如下图所示. 3.点击[显示]选项,进入控制面板显示界面,在左侧菜单栏里可以找的[调整亮度]选项,如下图所示. 4.点击[调整亮度]选项,进入控制面板电源选项界面,在该界面的左侧菜单栏里就可以找到[选择关闭显示器的时间]选项了,如下图所示. 5.点击[选择关闭显示器的时间]选项,进