如何以编程方式更改特定监视器的分辨率? [英] How to programmatically change the resolution of a specific monitor?

查看:70
本文介绍了如何以编程方式更改特定监视器的分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何以编程方式更改特定显示器的分辨率?例如,可以通过编程方式更改辅助监视器的分辨率吗?

How do you programmatically change the resolution of a specific monitor? For instance can the secondary monitor resolution be programmatically changed?

推荐答案

以下功能可能是您的起点.它将尝试使用由 Index 参数指定的索引(如果存在)将显示设备的分辨率更改为由 Width 指定的宽度和高度(以像素为单位), Height 参数.如果找到具有给定索引的显示设备并且其分辨率已成功更改,则该函数返回True,否则返回False.

The following function might be your starting point. It tries to change the resolution of a display device with index specified by the Index parameter (if exists such) to a width and height (in pixels) given by the Width, Height parameters. The function returns True, if the display device with given index is found and the resolution of it has been successfully changed, False otherwise.

您尚未指定是要永久更改分辨率(如果要存储设置更改)还是仅临时更改分辨率.下面的示例临时执行此操作,但是如果在第二个

You haven't specified whether you want to change the resolution permanently (if you want to store the setting changes), or change it only temporarily. The following example does it temporarily, but you can quite simply change this behavior if you use in the second ChangeDisplaySettingsEx function call the CDS_UPDATEREGISTRY value for the dwflags parameter:

function ChangeMonitorResolution(Index, Width, Height: DWORD): Boolean;
var
  DeviceMode: TDeviceMode;
  DisplayDevice: TDisplayDevice;
begin
  Result := False;
  ZeroMemory(@DisplayDevice, SizeOf(DisplayDevice));
  DisplayDevice.cb := SizeOf(TDisplayDevice);
  // get the name of a device by the given index
  if EnumDisplayDevices(nil, Index, DisplayDevice, 0) then
  begin
    ZeroMemory(@DeviceMode, SizeOf(DeviceMode));
    DeviceMode.dmSize := SizeOf(TDeviceMode);
    DeviceMode.dmPelsWidth := Width;
    DeviceMode.dmPelsHeight := Height;
    DeviceMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
    // check if it's possible to set a given resolution; if so, then...
    if (ChangeDisplaySettingsEx(PChar(@DisplayDevice.DeviceName[0]), 
      DeviceMode, 0, CDS_TEST, nil) = DISP_CHANGE_SUCCESSFUL)
    then
      // change the resolution temporarily (if you use CDS_UPDATEREGISTRY
      // value for the penultimate parameter, the graphics mode will also
      // be saved to the registry under the user's profile; for more info
      // see the ChangeDisplaySettingsEx reference, dwflags parameter)
      Result := ChangeDisplaySettingsEx(PChar(@DisplayDevice.DeviceName[0]),
        DeviceMode, 0, 0, nil) = DISP_CHANGE_SUCCESSFUL;
  end;
end;

如何将辅助显示设备(索引为1的设备)的分辨率更改为800x600的示例:

An example how to change resolution of a secondary display device (device with index 1) to 800x600:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ChangeMonitorResolution(1, 800, 600) then
    ShowMessage('Resolution of display device with index 1 has been changed!')
  else
    ShowMessage('Display device with index 1 doesn''t exist, doesn''t support ' +
      'resolution 800x600 or changing failed due to a reason, which you might ' +
      'know if the author of this function wouldn''t be so lazy!');
end;

这篇关于如何以编程方式更改特定监视器的分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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