切换显示器的横竖屏_Delphi_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Delphi > 切换显示器的横竖屏

切换显示器的横竖屏

 2011/7/26 8:03:23    程序员俱乐部  我要评论(0)
  • 摘要:unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ShellAPI;typeTForm1=class(TForm)btnCounterClockwise90Degree:TButton;btnRevert:TButton;btnClockwise90Degree:TButton;btnClockwise180Degree
  • 标签:显示器横竖屏
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    btnCounterClockwise90Degree: TButton;
    btnRevert: TButton;
    btnClockwise90Degree: TButton;
    btnClockwise180Degree: TButton;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    procedure btnCounterClockwise90DegreeClick(Sender: TObject);
    procedure btnRevertClick(Sender: TObject);
    procedure btnClockwise90DegreeClick(Sender: TObject);
    procedure btnClockwise180DegreeClick(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
const
  ENUM_CURRENT_SETTINGS = DWORD(-1);
  DMDO_DEFAULT = 0;
  DMDO_90 = 1;
  DMDO_180 = 2;
  DMDO_270 = 3;

type
  PDeviceMode = ^TDeviceMode;
  TDeviceMode = packed record
    dmDeviceName: array[0..CCHDEVICENAME - 1] of AnsiChar;
    dmSpecVersion: Word;
    dmDriverVersion: Word;
    dmSize: Word;
    dmDriverExtra: Word;
    dmFields: DWORD;
    dmPosition: TPointL;
    dmDisplayOrientation: DWORD;
    dmDisplayFixedOutput: DWORD;
    dmColor: SHORT;
    dmDuplex: SHORT;
    dmYResolution: SHORT;
    dmTTOption: SHORT;
    dmCollate: SHORT;
    dmFormName: array[0..CCHFORMNAME - 1] of AnsiChar;
    dmLogPixels: Word;
    dmBitsPerPel: DWORD;
    dmPelsWidth: DWORD;
    dmPelsHeight: DWORD;
    dmDisplayFlags: DWORD;
    dmDisplayFrequency: DWORD;
    dmICMMethod: DWORD;
    dmICMIntent: DWORD;
    dmMediaType: DWORD;
    dmDitherType: DWORD;
    dmICCManufacturer: DWORD;
    dmICCModel: DWORD;
    dmPanningWidth: DWORD;
    dmPanningHeight: DWORD;
  end;

function ChangeDisplaySettings(lpDevMode: PDeviceMode; dwFlags: DWORD): Longint;
  stdcall; external user32 name 'ChangeDisplaySettingsA';

function EnumDisplaySettings(lpszDeviceName: PChar; iModeNum: DWORD;
  lpDevMode: PDeviceMode): BOOL; stdcall; external user32 name 'EnumDisplaySettingsA';

procedure TForm1.btnCounterClockwise90DegreeClick(Sender: TObject);
var
  dm: TDeviceMode;
  dwTemp: DWORD;
begin
  ZeroMemory(@dm, SizeOf(dm));
  dm.dmSize := SizeOf(dm);
  dm.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
  if (EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, @dm)) then
  begin
    if dm.dmDisplayOrientation <> DMDO_90 then
    begin
      if dm.dmDisplayOrientation in [DMDO_DEFAULT, DMDO_180] then
      begin
        dwTemp := dm.dmPelsHeight;
        dm.dmPelsHeight := dm.dmPelsWidth;
        dm.dmPelsWidth := dwTemp;
        dm.dmDisplayOrientation := DMDO_90;
      end;
      dm.dmDisplayOrientation := DMDO_90;
      ChangeDisplaySettings(@dm, 0);

   //   dm.dmPelsWidth := 1024;
   //   dm.dmPelsHeight := 768;
  dm.dmDisplayOrientation := DMDO_180;
      ChangeDisplaySettings(@dm, 0); //设置新的显示参数

    end;
  end;

end;

procedure TForm1.btnRevertClick(Sender: TObject);
var
  dm: TDeviceMode;
  dwTemp: DWORD;
begin
  ZeroMemory(@dm, SizeOf(dm));
  dm.dmSize := SizeOf(dm);
  if (EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, @dm)) then
  begin
    if dm.dmDisplayOrientation <> DMDO_DEFAULT then
    begin
      if dm.dmDisplayOrientation in [DMDO_90, DMDO_270] then
      begin
        dwTemp := dm.dmPelsHeight;
        dm.dmPelsHeight := dm.dmPelsWidth;
        dm.dmPelsWidth := dwTemp;
      end;
      dm.dmDisplayOrientation := DMDO_DEFAULT;
      ChangeDisplaySettings(@dm, 0);
    end;
  end;

end;

procedure TForm1.btnClockwise90DegreeClick(Sender: TObject);
var
  dm: TDeviceMode;
  dwTemp: DWORD;
begin
  ZeroMemory(@dm, SizeOf(dm));
  dm.dmSize := SizeOf(dm);
  if (EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, @dm)) then
  begin
    if dm.dmDisplayOrientation <> DMDO_270 then
    begin
      if dm.dmDisplayOrientation in [DMDO_DEFAULT, DMDO_180] then
      begin
        dwTemp := dm.dmPelsHeight;
        dm.dmPelsHeight := dm.dmPelsWidth;
        dm.dmPelsWidth := dwTemp;
      end;
      dm.dmDisplayOrientation := DMDO_270;
      ChangeDisplaySettings(@dm, 0);
    end;
  end;

end;

procedure TForm1.btnClockwise180DegreeClick(Sender: TObject);
var
  dm: TDeviceMode;
  dwTemp: DWORD;
begin
  ZeroMemory(@dm, SizeOf(dm));
  dm.dmSize := SizeOf(dm);
  if (EnumDisplaySettings(nil, ENUM_CURRENT_SETTINGS, @dm)) then
  begin
    if dm.dmDisplayOrientation <> DMDO_180 then
    begin
      if dm.dmDisplayOrientation in [DMDO_90, DMDO_270] then
      begin
        dwTemp := dm.dmPelsHeight;
        dm.dmPelsHeight := dm.dmPelsWidth;
        dm.dmPelsWidth := dwTemp;
      end;
      dm.dmDisplayOrientation := DMDO_180;
      ChangeDisplaySettings(@dm, 0);
    end;
  end;

end;


procedure TForm1.btn2Click(Sender: TObject);
var
  sx, sy: Integer;
  DevMode: TDeviceMode;
begin
  sx := GetSystemMetrics(SM_CXSCREEN); //分辨率宽
  sy := GetSystemMetrics(SM_CYSCREEN); //分辨率高
   //用于在程序结束时把分辨率改回原始状态
  if (sx <> 1024) and (sy <> 768) then
  begin
    DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
    DevMode.dmPelsWidth := 1024;
    DevMode.dmPelsHeight := 768;
    DevMode.dmDisplayOrientation := DMDO_90;
  //  ChangeDisplaySettings(@DevMode,0); //设置新的显示参数
  end;

end;

procedure ChangeOrientation(NewOrientation: DWORD);
var
  dm: TDeviceMode;
  dwTemp: DWORD;
begin
  ZeroMemory(@dm, sizeof(dm));
  dm.dmSize := sizeof(dm);

  Pdevicemode(@dm)^.dmDisplayOrientation := NewOrientation;
 ChangeDisplaySettings(@dm,0);
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
ChangeOrientation(DMDO_180);
end;

end.
  • 相关文章
发表评论
用户名: 匿名