***************************************************************** * Neveprise Inc. Delphi FAQ * ***************************************************************** * Section: Desktop Topic: Set screen resolution * * Created: July 27th, 1999 Revised: July 27th, 1999 * * Updates: www.neveprise.de Info: support@neveprise.de * ***************************************************************** Q: How can I set another screen resolution? A: A bit complicated, but we can use the WinAPI function Change- DisplaySettings(): interface TStructH_ScreenMode = record //let us declare a custom structure... ResolutionX: integer; //"0" causes no change! ResolutionY: integer; ColorDepth: (Color16, Color256, HiColor, TrueColor, NoChange); SwitchMode: (srTest, srDynamic, srAsDefault, srRestore); Rate: integer; end; implementation function SetScreenResH(var Mode: TStructH_ScreenMode): integer; var DisSet: TDevMode; //device mode cResult: variant; //result from ChangeDisplaySettings() begin Result:=-1; DisSet.dmSize:=SizeOf(DisSet); //init structure DisSet.dmDisplayFlags:=0; //no special flags with DisSet do begin dmPelsWidth:=Mode.ResolutionX; //set width dmPelsHeight:=Mode.ResolutionY; //set height dmDisplayFrequency:=Mode.Rate; //set frequency //now the colour depth in bits per pixel if Mode.ColorDepth=Color16 then dmBitsPerPel:=4; if Mode.ColorDepth=Color256 then dmBitsPerPel:=8; if Mode.ColorDepth=HiColor then dmBitsPerPel:=16; if Mode.ColorDepth=HiColor then dmBitsPerPel:=32; if Mode.ResolutionX<>0 then //0 means to set only other values, e.g. colours DisSet.dmFields:=dm_PelsWidth+dm_PelsHeight+dm_DisplayFlags; //set valid fields if Mode.ColorDepth<>NoChange then //colour should not be set... DisSet.dmFields:=dmFields+dm_BitsPerPel; if Mode.Rate<>0 then //0 means not to set refresh rate (recommended!!!) DisSet.dmFields:=DisSet.dmFields+dm_DisplayFrequency; end; case Mode.SwitchMode of //now check what we want to do //only for this session, shall be used srDynamic: cResult:=ChangeDisplaySettings(DisSet,0); //shall be used everytime (e.g. when rebooting still these settings) srAsDefault: cResult:=ChangeDisplaySettings(DisSet,cds_UpDateRegistry); //nothing shall be done, we just want to test if it is possible to set these modes srTest: cResult:=ChangeDisplaySettings(DisSet,cds_Test); //reset to defaults (will not work if srAsDefault!) srRestore: begin with DisSet do begin //make invalid params dmFields:=dm_PelsWidth+dm_PelsHeight+dm_DisplayFlags+dm_BitsPerPel; dmPelsWidth:=0; dmPelsHeight:=0; dmBitsPerPel:=0; cResult:=ChangeDisplaySettings(DisSet,cds_UpDateRegistry); end; end; end; case cResult of Disp_Change_Successful: Result:=0; //all okay, mode was set / can be set Disp_Change_Restart: Result:=1; //would work, but only if reboot is performed Disp_Change_BadFlags: Result:=3; //we used wrong parameters, e.g. 311 as width Disp_Change_Failed: Result:=3; //some error occured Disp_Change_BadMode: Result:=3; //could have worked, but it is very unstable Disp_Change_NotUpDated: Result:=2; //could not store as default, but worked for now end; end; Blazko