如何在不连接显示器的情况下设置屏幕分辨率?



在我的公司,我们有时会使用表面自动化来自动化我们的流程。这意味着,程序正在屏幕上搜索图像并单击它们,例如:

我们正在开发使用屏幕分辨率2560 x 1440.我们有许多计算机在那里为我们工作 - 并非所有计算机都始终连接到显示器。当我断开显示器的连接时,窗口正在将分辨率更改为1024 x 768.我想避免这种情况。在互联网上,我读到了关于NirCmd工具的信息 - 但是在我的大型保守公司中使用这样的工具对我来说并不容易,我想知道C#是否有可能。

我发现一个解决方案运行良好 - 但前提是连接了显示器(在此请求末尾找到代码)。即使没有连接显示器,是否可以更改分辨率?或者是否有可能一劳永逸地定义分辨率,Windows在没有连接屏幕的情况下使用?这对我不起作用: https://superuser.com/questions/604026/how-to-set-default-screen-resolution-and-color-depth-without-monitor-in-windows

工作正常的代码 - 但仅在连接屏幕时:

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
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 = 32)]
public string dmFormName;
public short dmLogPixels;
public short dmBitsPerPel;
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;
};
class User_32
{
[DllImport("user32.dll")]
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
[DllImport("user32.dll")]
public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
public const int ENUM_CURRENT_SETTINGS = -1;
public const int CDS_UPDATEREGISTRY = 0x01;
public const int CDS_TEST = 0x02;
public const int DISP_CHANGE_SUCCESSFUL = 0;
public const int DISP_CHANGE_RESTART = 1;
public const int DISP_CHANGE_FAILED = -1;
}
public static class ScreenResolution
{
/// <summary>
/// Ändert die Bildschirmauflösung in die gewünschte Größe
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public static void ChangeResolution(int width, int height)
{
Screen screen = Screen.PrimaryScreen;
int iWidth = width;
int iHeight = height;
DEVMODE1 dm = new DEVMODE1();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
{
dm.dmPelsWidth = iWidth;
dm.dmPelsHeight = iHeight;
int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
if (iRet == User_32.DISP_CHANGE_FAILED)
{
MessageBox.Show("Unable to process your request");
MessageBox.Show("Description: Unable To Process Your Request. Sorry For This Inconvenience.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
switch (iRet)
{
case User_32.DISP_CHANGE_SUCCESSFUL:
{
break;
//successfull change
}
case User_32.DISP_CHANGE_RESTART:
{
MessageBox.Show("Description: You Need To Reboot For The Change To Happen.n If You Feel Any Problem After Rebooting Your MachinenThen Try To Change Resolution In Safe Mode.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
//windows 9x series you have to restart
}
default:
{
MessageBox.Show("Description: Failed To Change The Resolution.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
//failed to change
}
}
}
}
}
}

要以编程方式更改屏幕分辨率,请使用 ChangeDisplaySettings()。

语法:

LONG ChangeDisplaySettings(
_In_ DEVMODE *lpDevMode,
_In_ DWORD   dwflags
);

如果硬件解决方案对您来说可行,您可以使用名为 video edid(是的,拼写正确)的设备。它是一个记忆棒大小的小加密狗,放在显卡和视频电缆之间。它模拟操作系统的运行屏幕,因此始终保持最新的分辨率。谷歌的"视频edid",你会发现产品的价格约为20美元

最新更新