Recent

Author Topic: Fun code to rotate your desktop  (Read 299 times)

Ten_Mile_Hike

  • Full Member
  • ***
  • Posts: 149
Fun code to rotate your desktop
« on: June 19, 2026, 09:42:42 pm »
Code: Text  [Select][+][-]
  1. The following code will rotate your desktop through
  2. 90/180/270 degrees and finally back to normal.
  3.  
  4. Code is provided as is for entertainment only.
  5.  
  6. It works on my Win11 laptop. I cannot tell you if
  7. it will work with all graphics cards or another OS
  8.  
  9. In the unlikely event that Windows has a hiccup your
  10. display may get "stuck" at an angle that WILL PERSIST
  11. EVEN AFTER A REBOOT. Should this happen you must
  12. go to Display settings>Display Orientation>Landscape
  13. and all will be well. I purposely made Win 11 crash
  14. part way through and this intervention worked easily
  15. (once I could work mouse on upside down display.)
  16.  



Code: Pascal  [Select][+][-]
  1. unit Unit1;{$mode objfpc}{$H+}interface
  2. uses Windows, SysUtils, Forms, StdCtrls;
  3. type
  4.   TForm1 = class(TForm)
  5.     Button1: TButton;
  6.     procedure Button1Click(Sender: TObject);
  7.     end;
  8. var Form1: TForm1;implementation  {$R *.lfm}
  9.  
  10. Procedure RestoreDisplay;
  11. var  DevMode: TDeviceMode;
  12. begin
  13.   ZeroMemory(@DevMode, SizeOf(DevMode));
  14.   DevMode.dmSize := SizeOf(DevMode);
  15.  
  16.   if EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, DevMode) then
  17.   begin
  18.     DevMode.dmDisplayOrientation := DMDO_DEFAULT;
  19.     DevMode.dmFields :=
  20.       DM_DISPLAYORIENTATION or DM_PELSWIDTH or DM_PELSHEIGHT;
  21.  
  22.     ChangeDisplaySettingsEx(nil, DevMode, 0, CDS_UPDATEREGISTRY, nil);
  23.   end;
  24. end;
  25.  
  26. Procedure TForm1.Button1Click(Sender: TObject);
  27. var
  28.   DevMode: TDeviceMode;
  29.   W, H: LongInt;
  30. begin
  31.   ZeroMemory(@DevMode, SizeOf(DevMode));
  32.   DevMode.dmSize := SizeOf(DevMode);
  33.  
  34.   if EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, DevMode) then
  35.   begin
  36.     W := DevMode.dmPelsWidth;
  37.     H := DevMode.dmPelsHeight;
  38.  
  39.     DevMode.dmFields :=
  40.       DM_DISPLAYORIENTATION or DM_PELSWIDTH or DM_PELSHEIGHT;
  41.  
  42.     DevMode.dmDisplayOrientation := DMDO_270;
  43.     DevMode.dmPelsWidth := H;
  44.     DevMode.dmPelsHeight := W;
  45.     ChangeDisplaySettingsEx(nil, DevMode, 0, CDS_UPDATEREGISTRY, nil);
  46.     Sleep(1500);
  47.  
  48.     DevMode.dmDisplayOrientation := DMDO_180;
  49.     DevMode.dmPelsWidth := W;
  50.     DevMode.dmPelsHeight := H;
  51.     ChangeDisplaySettingsEx(nil, DevMode, 0, CDS_UPDATEREGISTRY, nil);
  52.     Sleep(1500);
  53.  
  54.     DevMode.dmDisplayOrientation := DMDO_90;
  55.     DevMode.dmPelsWidth := H;
  56.     DevMode.dmPelsHeight := W;
  57.     ChangeDisplaySettingsEx(nil, DevMode, 0, CDS_UPDATEREGISTRY, nil);
  58.     Sleep(1500);
  59.  
  60.     DevMode.dmDisplayOrientation := DMDO_DEFAULT;
  61.     DevMode.dmPelsWidth := W;
  62.     DevMode.dmPelsHeight := H;
  63.     ChangeDisplaySettingsEx(nil, DevMode, 0, CDS_UPDATEREGISTRY, nil);
  64.  
  65.     //RestoreDisplay; //Redundant... for safety only
  66.   end;
  67. end;
  68. end.
  69.  
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

jcmontherock

  • Sr. Member
  • ****
  • Posts: 361
Re: Fun code to rotate your desktop
« Reply #1 on: June 21, 2026, 12:25:42 pm »
Thanks a lot. It's work fine for me. I was looking for such an example program for a long time.
« Last Edit: June 21, 2026, 12:31:57 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 4.8-64 - FPC 3.2.2

 

TinyPortal © 2005-2018