1
votes

How can I detect screen resolution change in Delphi FireMonkey / FMX

This solution for VCL has already been posted How to detect screen resolution change in Delphi?

That solution works well for VCL, but I am looking for a solution for FireMonkey.

1
For which platforms?Dave Nottage
If you are interested only on windows platform then the linked answer should also bee good enough as it relyies on intercepting Windows message that is send at screen resolution change by the OS itself. I'm not sure if same would work on other platforms.SilverWarior
Windows api code won't work on Android. Use the onresize method of the form on Android.nolaspeaker
@nolaspeaker Can you even adjust screen resolution on android devices? Isn't screen resolution of all android devices fixed to the screen resolution of physical screen?SilverWarior
Yes. But differentAndroid devices have different screen metrics, so you might want to determine the height and width of the screen for control element positioning, especially after the device is rotated.nolaspeaker

1 Answers

0
votes

Try this codes. First add FMX.Platform to uses:

function ScreenResolutionChanged(old, new: string): Boolean;
begin
  if old <> new then
    Result := True
  else
    Result := False;
end;

function getScreenSize: string;
var
  ScreenSvc: IFMXScreenService;
  ScreenSize: TSize;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService,
    IInterface(ScreenSvc)) then
  begin
    Result := Format('Resolution: %fX%f', [ScreenSvc.GetScreenSize.X,
        ScreenSvc.GetScreenSize.Y]);
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  oldScreenSize, newScreenSize: string;
begin
  oldScreenSize := getScreenSize;
  Sleep(10000); // Wait 10 Sec
  newScreenSize := getScreenSize;
  if ScreenResolutionChanged(newScreenSize, oldScreenSize) then
    ShowMessage('Screen Resolution Changed ' + #10#13 + oldScreenSize + #10#13 +
      newScreenSize)
  else
    ShowMessage('Screen Resolution NOT Changed ' + #10#13 + oldScreenSize +
      #10#13 + newScreenSize);
end;