首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何以编程方式更改特定显示器的分辨率?

如何以编程方式更改特定显示器的分辨率?
EN

Stack Overflow用户
提问于 2013-02-27 08:17:58
回答 1查看 3.8K关注 0票数 3

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 14:07:23

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

您尚未指定是永久更改分辨率(如果要存储设置更改),还是仅临时更改。下面的示例暂时执行此操作,但如果在第二个ChangeDisplaySettingsEx函数中使用调用dwflags参数的CDS_UPDATEREGISTRY值,则可以非常简单地更改此行为:

代码语言:javascript
运行
复制
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的示例:

代码语言:javascript
运行
复制
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;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15101977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档