3

特定のモニターの解像度をプログラムでどのように変更しますか? たとえば、セカンダリ モニターの解像度をプログラムで変更できますか?

4

1 に答える 1

5

次の関数が出発点になる可能性があります。パラメータで指定されたインデックス (存在する場合) を持つディスプレイ デバイスの解像度を、パラメータで指定された幅と高さ (ピクセル単位) に変更しようとしIndexます。この関数は、指定されたインデックスを持つディスプレイ デバイスが見つかり、その解像度が正常に変更された場合に True を返し、それ以外の場合は False を返します。WidthHeight

解像度を永続的に変更するか (設定の変更を保存する場合)、一時的にのみ変更するかを指定していません。次の例では一時的にこれを行っていますが、2 番目のChangeDisplaySettingsEx関数呼び出しでパラメーターのCDS_UPDATEREGISTRY値を使用すると、この動作を非常に簡単に変更できます。dwflags

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 に変更する方法の例:

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;
于 2013-02-27T06:07:22.620 に答える