如何在 C# 中为 Windows-7 设置主监视器 [英] how to set primary monitor for Windows-7, in C#

查看:26
本文介绍了如何在 C# 中为 Windows-7 设置主监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我需要一个在 SetDisplayConfig() 中执行此操作的示例.

I believe I will need an example of doing this in SetDisplayConfig().

我的 Windows-7 系统有两个显示器.当我的程序处于一种模式时,第一个监视器必须打开并且是主要的,第二个监视器必须关闭.在另一种模式下,反之亦然:第一个显示器关闭,第二个显示器打开和主要.

My Windows-7 system has two monitors. When my program is in one mode, the 1st monitor must be on and primary, and the 2nd monitor off. In the other mode, vice versa: 1st monitor off, 2nd monitor on and primary.

我已经搜索并搜索了如何使用 Windows SDK 函数SetDisplayConfig()"来执行此操作,但一无所获.MSDN 对 SetDisplayConfig() 的引用对我来说太深奥了,而且没有示例代码.

I've searched and searched for how to do it with Windows SDK function "SetDisplayConfig()" but found nothing. The MSDN reference to SetDisplayConfig() is too esoteric for me, and has no example code.

我使用 ChangeDisplaySettingsEx() 实现了它,但是这个函数在 Windows-7 中是不稳定的.

I got it going using ChangeDisplaySettingsEx(), but this function is flaky in Windows-7.

谢谢!

推荐答案

我目前也在同时使用 SetDisplayConfig()ChangeDisplaySettingsEx() 并发现这似乎使用我的设置.SDC_TOPOLOGY_INTERNALSDC_TOPOLOGY_EXTERNAL 指的是 Windows 决定您的主要(屏幕)和次要(投影仪)显示器是什么,类似于按下 Win+P.对我来说则相反,因此您必须检查配置中的正确配置是什么.然后您可以简单地调用 InternalDisplay()ExternalDisplay() 来激活一个并自动停用另一个.为了完整起见,我添加了克隆和扩展设置.

I currently fiddle with both SetDisplayConfig() and ChangeDisplaySettingsEx() as well and found that this seems to work with my setup. SDC_TOPOLOGY_INTERNAL and SDC_TOPOLOGY_EXTERNAL refer to whatever Windows decides your primary (screen) and secondary (projector) monitor is, similar to the monitor selection when your press Win+P. It's the other way around for me, so you'd have to check whats the correct one in your configuration is. Then you can simply call InternalDisplay() or ExternalDisplay() to activate the one and automatically deactivate the other. I added the clone and extend settings for the sake of completeness.

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern long SetDisplayConfig(uint numPathArrayElements,
IntPtr pathArray, uint numModeArrayElements, IntPtr modeArray, uint flags);

UInt32 SDC_TOPOLOGY_INTERNAL = 0x00000001;
UInt32 SDC_TOPOLOGY_CLONE = 0x00000002;
UInt32 SDC_TOPOLOGY_EXTEND = 0x00000004;
UInt32 SDC_TOPOLOGY_EXTERNAL = 0x00000008;
UInt32 SDC_APPLY = 0x00000080;

public void CloneDisplays() {
  SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_CLONE));
}

public void ExtendDisplays() {
  SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_EXTEND));
 }

public void ExternalDisplay() {
  SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_EXTERNAL));
}

public void InternalDisplay() {
  SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, (SDC_APPLY | SDC_TOPOLOGY_INTERNAL));
}

这篇关于如何在 C# 中为 Windows-7 设置主监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆