delphi实现电脑屏幕旋转(电脑屏幕,不是手机屏幕)

相关资料:

 

注意:是电脑屏幕,不是手机屏幕

实例代码:

 1 unit Unit10;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
 8 
 9 type
10   TForm10 = class(TForm)
11     ComboBox1: TComboBox;
12     Label1: TLabel;
13     Button1: TButton;
14     procedure Button1Click(Sender: TObject);
15   private
16     { Private declarations }
17   public
18     { Public declarations }
19   end;
20 const
21   DM_DISPLAYORIENTATION = $00800000;
22   ENUM_CURRENT_SETTINGS =-1;
23   DMDO_DEFAULT : DWORD  = 0;
24        DMDO_90 : DWORD  = 1;
25       DMDO_180 : DWORD  = 2;
26       DMDO_270 : DWORD  = 3;
27 var
28   Form10: TForm10;
29 
30 implementation
31 
32 {$R *.dfm}
33 procedure ChangeOrientation(NewOrientation: DWORD);
34 var
35   dm: TDeviceMode;
36   dwTemp: DWORD;
37   dmDisplayOrientation : DWORD;
38 begin
39    ZeroMemory(@dm, sizeof(dm));
40    dm.dmSize := sizeof(dm);
41    if EnumDisplaySettings(nil, DWORD(ENUM_CURRENT_SETTINGS), dm) then
42    begin
43       Move(dm.dmScale,dmDisplayOrientation, SizeOf(dmDisplayOrientation));
44       // swap width and height
45       if Odd(dmDisplayOrientation) <> Odd(NewOrientation) then
46       begin
47        dwTemp := dm.dmPelsHeight;
48        dm.dmPelsHeight := dm.dmPelsWidth;
49        dm.dmPelsWidth := dwTemp;
50       end;
51       if dmDisplayOrientation <> NewOrientation then
52       begin
53         Move(NewOrientation, dm.dmScale, SizeOf(NewOrientation));
54         if (ChangeDisplaySettings(dm, 0) <> DISP_CHANGE_SUCCESSFUL) then
55          RaiseLastOSError;
56       end;
57    end;
58 end;
59 
60 procedure TForm10.Button1Click(Sender: TObject);
61 begin
62   try
63     case ComboBox1.ItemIndex of
64       0: begin
65         ChangeOrientation(DMDO_180);
66         ShowMessage('旋转180度');
67       end;
68       1: begin
69         ChangeOrientation(DMDO_270);
70         ShowMessage('旋转270度');
71       end;
72       2: begin
73         ChangeOrientation(DMDO_90);
74         ShowMessage('旋转90度');
75       end;
76       3: begin
77         ChangeOrientation(DMDO_DEFAULT);
78         ShowMessage('默认值');
79       end;
80     end;
81   except
82     on E: Exception do
83       ShowMessage(E.ClassName + ':' +E.Message);
84   end;
85 end;
86 
87 end.
View Code

 

posted on 2020-04-07 17:21  疯狂delphi  阅读(583)  评论(0编辑  收藏  举报

导航