gpt4 book ai didi

c# - 以编程方式断开和重新连接显示

转载 作者:行者123 更新时间:2023-11-30 16:57:39 29 4
gpt4 key购买 nike

问题以编程方式断开和重新连接显示的最佳方式是什么?

目标:关闭显示器上的视频输出(无背光的黑屏),然后重新打开。想象一下,从显示器上拔下视频线,然后重新插入。

我的尝试:

// Get the monitor to disable
uint iDevNum = 0;
DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
displayDevice.cb = Marshal.SizeOf(displayDevice);
EnumDisplayDevices(null, iDevNum, ref displayDevice, 0))

DEVMODE devMode = new DEVMODE();
EnumDisplaySettings(displayDevice.DeviceName, 0, ref devMode);

//
// Do something here to disable this display device!
//

// Save the display settings
ChangeDisplaySettingsEx(displayDevice.DeviceName, ref devMode,
IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_NONE, IntPtr.Zero);

我可以与每个显示器交互,但我不知道如何断开连接。

它类似于 Windows 7 中屏幕分辨率属性中的“断开此显示器”:

Windows 7 Screen Resolution Properties

注意事项:

  • 关闭所有显示器的视频输出是行不通的,因为我需要其他显示器保持打开状态。
  • “死”显示器上的桌面区域在关闭时不需要可用。另外,如果 window 四处移动也没关系。

引用资料:

  1. SO: Enabling a Second Monitor
  2. How to Turn Off a Monitor

最佳答案

1) 从这里获取 MultiMonitorHelper: https://github.com/ChrisEelmaa/MultiMonitorHelper/tree/master

2) 扩展 Win7Display断开显示:

using MultiMonitorHelper.DisplayModels.Win7.Enum;
using MultiMonitorHelper.DisplayModels.Win7.Struct;

/// <summary>
/// Disconnect a display.
/// </summary>
public void DisconnectDisplay(int displayNumber)
{
// Get the necessary display information
int numPathArrayElements = -1;
int numModeInfoArrayElements = -1;
StatusCode error = CCDWrapper.GetDisplayConfigBufferSizes(
QueryDisplayFlags.OnlyActivePaths,
out numPathArrayElements,
out numModeInfoArrayElements);

DisplayConfigPathInfo[] pathInfoArray = new DisplayConfigPathInfo[numPathArrayElements];
DisplayConfigModeInfo[] modeInfoArray = new DisplayConfigModeInfo[numModeInfoArrayElements];
error = CCDWrapper.QueryDisplayConfig(
QueryDisplayFlags.OnlyActivePaths,
ref numPathArrayElements,
pathInfoArray,
ref numModeInfoArrayElements,
modeInfoArray,
IntPtr.Zero);
if (error != StatusCode.Success)
{
// QueryDisplayConfig failed
}

// Check the index
if (pathInfoArray[displayNumber].sourceInfo.modeInfoIdx < modeInfoArray.Length)
{
// Disable and reset the display configuration
pathInfoArray[displayNumber].flags = DisplayConfigFlags.Zero;
error = CCDWrapper.SetDisplayConfig(
pathInfoArray.Length,
pathInfoArray,
modeInfoArray.Length,
modeInfoArray,
(SdcFlags.Apply | SdcFlags.AllowChanges | SdcFlags.UseSuppliedDisplayConfig));
if (error != StatusCode.Success)
{
// SetDisplayConfig failed
}
}
}

3) 使用来自 this post 的答案扩展 Win7Display重新连接显示器 :

using System.Diagnostics;

/// <summary>
/// Reconnect all displays.
/// </summary>
public void ReconnectDisplays()
{
DisplayChanger.Start();
}

private static Process DisplayChanger = new Process
{
StartInfo =
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "DisplaySwitch.exe",
Arguments = "/extend"
}
};

4)更新IDisplay中的方法。

5)实现方法:

IDisplayModel displayModel = DisplayFactory.GetDisplayModel();
List<IDisplay> displayList = displayModel.GetActiveDisplays().ToList();

displayList[0].DisconnectDisplay(0);
displayList[0].ReconnectDisplays();

关于c# - 以编程方式断开和重新连接显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26169268/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com