2004. november 19., péntek

How to change the screen resolution programmatically


Problem/Question/Abstract:

How to change the screen resolution programmatically

Answer:

Solve 1:

To change the screen resolution on a user's PC is bad programming style, but anyway:

{ ... }
type
  TDisplaySetting = record
    dsWidth, dsHeight, dsBPP: Cardinal {DWORD};
  end;
  { ... }

function ChangeDisplayMode(var Settings: TDisplaySetting): Boolean;
var
  I: Integer;
  DC: HDC;
  DevMode: TDevMode;
  Current: TDisplaySetting;
begin
  Result := FALSE;
  { Enumerate available display modes }
  I := 0;
  while EnumDisplaySettings(nil, I, DevMode) do
  begin
    { Check if this is desired settings }
    if (DevMode.dmPelsWidth = Settings.dsWidth) and (DevMode.dmPelsHeight =
      Settings.dsHeight)
      and (DevMode.dmBitsPerPel = Settings.dsBPP) then
    begin
      { Get Current Display Settings }
      DC := GetDC(0);
      with Current do
      begin
        dsWidth := GetDeviceCaps(DC, HORZRES);
        dsHeight := GetDeviceCaps(DC, VERTRES);
        dsBPP := GetDeviceCaps(DC, BITSPIXEL);
      end;
      ReleaseDC(0, DC);
      { Indicate what settings to change }
      DevMode.dmFields := DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT;
      { Try to change settings }
      if ChangeDisplaySettings(DevMode, 0) = DISP_CHANGE_SUCCESSFUL then
      begin
        Result := TRUE;
        { Return previous settings if successful }
        Settings := Current;
      end;
      Break;
    end;
    Inc(I);
  end;
end;

Used like this:

var
  DS: TDisplaySettings;
begin
  DS.dsWidth := 800;
  DS.dsHeight := 600;
  DS.dsBPP := 24;
  { Change display mode }
  if ChangeDisplayMode(DS) then
  begin
    { DS now stores previous settings }
    { ... }
    { Restore original settings }
    ChangeDisplayMode(DS);
  end;
end;


Solve 2:

Here's a method I built to force 1024x768:

function SetDisplay1024x768: Boolean;
var
  DevMode: TDeviceMode;
begin
  EnumDisplaySettings(nil, 0, DevMode);
  DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
  DevMode.dmPelsWidth := 1024;
  DevMode.dmPelsHeight := 768;
  Result := ChangeDisplaySettings(DevMode, CDS_UPDATEREGISTRY)
    = DISP_CHANGE_SUCCESSFUL;
end;


Answer 3:

function ChangeRes(X, Y, Frequency, ColorDepth: Cardinal): boolean;
var
  RetCode: Integer;
  MyDevMode: TDeviceMode;
begin
  EnumDisplaySettings(nil, 0, MyDevMode);
  MyDevMode.dmDisplayFrequency := Frequency;
  MyDevMode.dmPelsWidth := X;
  MyDevMode.dmPelsHeight := Y;
  MyDevMode.dmBitsPerPel := ColorDepth;
  MyDevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL
    or DM_DISPLAYFREQUENCY;
  RetCode := ChangeDisplaySettings(MyDevMode, CDS_UPDATEREGISTRY);
  case RetCode of
    DISP_CHANGE_SUCCESSFUL, DISP_CHANGE_RESTART: Result := true;
  else
    Result := false;
  end;
  case RetCode of
    DISP_CHANGE_SUCCESSFUL:
      Writeln('DISP_CHANGE_SUCCESSFUL');
    DISP_CHANGE_RESTART:
      Writeln('DISP_CHANGE_RESTART');
    DISP_CHANGE_FAILED:
      Writeln('DISP_CHANGE_FAILED');
    DISP_CHANGE_BADMODE:
      Writeln('DISP_CHANGE_BADMODE');
    DISP_CHANGE_NOTUPDATED:
      Writeln('DISP_CHANGE_NOTUPDATED');
    DISP_CHANGE_BADFLAGS:
      Writeln('DISP_CHANGE_BADFLAGS');
    DISP_CHANGE_BADPARAM:
      Writeln('DISP_CHANGE_BADPARAM');
  else
    Writeln('DISP_CHANGE_OTHER_ERROR');
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése