2004. május 16., vasárnap

Set the resolution of your screen


Problem/Question/Abstract:

This article shows how to set the resolution of your screen I pasted my whole unit below.

Answer:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
    ExtCtrls, Buttons;

type
  TForm4 = class(TForm)
    ComboBox1: TComboBox;
    BitBtn1: TBitBtn;
    Bevel1: TBevel;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Button1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;
  Modes: array[0..255] of TDevMode;

implementation

uses cliprex2;

{$R *.DFM}

procedure TForm4.ComboBox1Change(Sender: TObject);
begin
  bitbtn1.Enabled := combobox1.ItemIndex >= 0;
  bitbtn1.enabled := true;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  DC: THandle;
  Bits: Integer;
  HRes: Integer;
  VRes: Integer;
  DM: TDevMode;
  ModeNum: LongInt;
  Ok: Bool;
  I: Byte;
begin

  DC := Canvas.Handle;
  Bits := GetDeviceCaps(DC, BITSPIXEL);
  HRes := GetDeviceCaps(DC, HORZRES);
  VRes := GetDeviceCaps(DC, VERTRES);

  ModeNum := 0;
  EnumDisplaySettings(nil, ModeNum, DM);
  Modes[ModeNum] := DM;
  Ok := True;
  while Ok do
  begin
    Inc(ModeNum);
    Ok := EnumDisplaySettings(nil, ModeNum, DM);
    Modes[ModeNum] := DM;
  end;

  for I := 0 to ModeNum - 1 do
  begin
    ComboBox1.Items.Add(Format('%d x %d, %d bits',
      [TDevMode(Modes[I]).dmPelsWidth,
      TDevMode(Modes[I]).dmPelsHeight,
        TDevMode(Modes[I]).dmBitsPerPel]));
    ComboBox1.ItemIndex := 0;
  end;
end;

procedure TForm4.FormActivate(Sender: TObject);

var
  DC: THandle;
  Bits: Integer;
  HRes: Integer;
  VRes: Integer;

begin

  DC := Canvas.Handle;
  Bits := GetDeviceCaps(DC, BITSPIXEL);
  HRes := GetDeviceCaps(DC, HORZRES);
  VRes := GetDeviceCaps(DC, VERTRES);

  combobox1.text := Format('%d x %d, %d bits', [HRes, VRes, Bits]);
  bitbtn1.enabled := false;
end;

procedure TForm4.Label4Click(Sender: TObject);
begin
  form4.hide;
end;

procedure TForm4.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bitbtn1.Font.Color := clblue;
end;

procedure TForm4.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bitbtn1.Font.Color := clblack;
end;

procedure TForm4.BitBtn1Click(Sender: TObject);
var
  NewMode: TDevMode;
  ChResult: LongInt;

begin
  NewMode := TDevMode(Modes[ComboBox1.ItemIndex]);
  NewMode.dmDisplayFrequency := 0;
  NewMode.dmDisplayFlags :=
    DM_BITSPERPEL and
    DM_PELSWIDTH and
    DM_PELSHEIGHT and
    DM_DISPLAYFLAGS;
  ChResult := ChangeDisplaySettings(NewMode, CDS_UPDATEREGISTRY);

  form4.hide;
end;

procedure TForm4.BitBtn1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bitbtn1.font.color := clblue;
end;

procedure TForm4.BitBtn1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bitbtn1.font.color := clblack;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése