5

In the link below, Microsoft describes two ways to limit rotation of an application screen on a tablet.

http://msdn.microsoft.com/en-ca/library/windows/apps/hh700342.aspx

what's happening is that delphi's (XE3) TRibbon doesn't handle rotation well. it tends to get hung.

as would be expected, the MS web site describes how to do this from MS development products. I don't see how I can do this in my Delphi project.

Method 1:

add this to your appxmanifest file:

<InitialRotationPreference>
    <Rotation Preference="landscape"/>
    <Rotation Preference="landscapeFlipped"/>
</InitialRotationPreference>

I haven't yet found where/how the appxmanifest should be part of the application so I can do this in delphi.

Method 2:

call this with code:

 Windows.Graphics.Display.DisplayProperties.AutoRotationPreferences =
            Windows.Graphics.Display.DisplayOrientations.Landscape;

to migrate this to delphi, I'd need to know API DLL information so I could do something similar.

Any ideas?

Could there be a COM object or DLL that gives us access to this?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
X-Ray
  • 2,816
  • 1
  • 37
  • 66
  • Although very doubtful, I'm still waiting for the day that Delphi is recognized enough for Microsoft to provide code examples just like their other primary languages. – Jerry Dodge Apr 19 '13 at 02:00
  • Can you please clarify whether you want this to apply specifically to Windows 8 or not? AFAIK, It's applicable to Windows 7+, but I am assuming it may be from XP+ – Jerry Dodge Apr 19 '13 at 02:05
  • I only had expected it to work with win8. thanks! jim mckeeth has given a solution below. – X-Ray Apr 19 '13 at 23:54

1 Answers1

6

Those calls are to disable rotation for a WindowsRT application (FKA Metro) which you cannot build with Delphi (yet). Even a Metropolis app is still a desktop app. There is a solution on the Intel site.

Based on feedback from X-Ray I cleaned up the code:

unit MetroDisplayRotation;

(* 
 *  Usage: TMetroDisplayRotation.SetDisplayAutoRotationPreferences(
 *           TMetroDisplayRotation.ORIENTATION_PREFERENCE_LANDSCAPE or 
 *           TMetroDisplayRotation.ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED);
 *)

interface

type
  TMetroDisplayRotation = class
  public const
    ORIENTATION_PREFERENCE_NONE = $0;
    ORIENTATION_PREFERENCE_LANDSCAPE = $1;
    ORIENTATION_PREFERENCE_PORTRAIT = $2;
    ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED = $4;
    ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED = $8;

    class procedure SetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE: Integer);
  end;

implementation

uses
  SysUtils, Windows;

{ TMetroDisplayRotation }

class procedure TMetroDisplayRotation.SetDisplayAutoRotationPreferences(
  ORIENTATION_PREFERENCE: Integer);
type
  TSDARP = procedure(ORIENTATION_PREFERENCE: Integer); stdcall;
var
  UserHandle: THandle;
  SDARP: TSDARP;
begin
  UserHandle := GetModuleHandle('User32.dll');
  @SDARP := GetProcAddress(UserHandle, 'SetDisplayAutoRotationPreferences');
  if Assigned(SDARP) then
    SDARP(ORIENTATION_PREFERENCE);
end;

end.

You will want to make sure you ONLY call this on Windows 8 since that procedure doesn't exist elsewhere.

Usage: TMetroDisplayRotation.SetDisplayAutoRotationPreferences(TMetroDisplayRotation.ORIENTATION_PREFERENCE_LANDSCAPE or TMetroDisplayRotation.ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED);

Another BAD option is to disable it for the entire tablet. Just go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AutoRotation in the registry and change Enable to 0.

Community
  • 1
  • 1
Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
  • THANK YOU, Jim! Had to add stdcall and then it worked. Also made a couple of other minor improvements. Having tested it thoroughly and considering that you can't test it, I felt it was right to edit your comment to include this change. Thank you for what you do for the Delphi community! – X-Ray Apr 19 '13 at 23:50
  • @X-Ray: Feel free to edit my code. Yeah, I totally spaced StdCall. – Jim McKeeth Apr 21 '13 at 00:18
  • Jim: my changes are not visible. stackoverflow said the changes would need to be "peer reviewed" and until then only i could see the changes. now even i don't see my changes so it would appear that never happened. thanks! mp – X-Ray Apr 21 '13 at 16:05
  • @X-Ray: Thanks, I cleaned it up and posted it as a reusable unit. – Jim McKeeth Apr 22 '13 at 20:23