Delphi的TScreen类

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.ScrollBars := ssBoth;
  Memo1.Align := alLeft;
  Memo1.Clear;

  {通过 Screen 对象, 只此一句话即可获取字体列表}
  Memo1.Lines := Screen.Fonts;
end;

end.

  
var
  w,h: Integer;
begin
  w := Screen.Width;
  h := Screen.Height;

  Text := Format('当前屏幕的分辨率是: %d*%d', [w,h]);
end;

Screen 对象是 TScreen 类的一个变量, 它声明在 Forms 单元的第 1323 行(Delphi 2007);
并且 TScreen 类也来自 Forms 单元.
也就是说, 只要 uses 了 Forms 单元, Screen 对象就可以使用了.

我们自己建立一个 TScreen 类的对象可以吗? 当然可以!


var
  MyScreen: TScreen;
begin
  MyScreen := TScreen.Create(nil); {建立}

  ShowMessage(IntToStr(MyScreen.Width)); {显示: 1024; 我的屏幕分辨率是 1024*768}

  MyScreen.Free;                   {释放}
  MyScreen := nil;                 {连根拔掉}
end;
//一个包含两个窗体的例子, 这是工程中的内容:
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

   
// unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses Unit2; procedure TForm1.Button1Click(Sender: TObject); var num: Integer; begin num := Screen.FormCount; {当前程序中窗体的个数} ShowMessage(IntToStr(num)); {2} Screen.Forms[1].ShowModal; {显示第二个窗体} {上面这句相当于} //Form2.ShowModal; end; end.
//更换窗体或某个控件的光标可以不通过 Screen 对象, 譬如:
begin
  Self.Cursor := crAppStart;
  Panel1.Cursor := crHandPoint;

{光标可选值:
  crDefault     = TCursor(0);
  crNone        = TCursor(-1);
  crArrow       = TCursor(-2);
  crCross       = TCursor(-3);
  crIBeam       = TCursor(-4);
  crSize        = TCursor(-22);
  crSizeNESW    = TCursor(-6);
  crSizeNS      = TCursor(-7);
  crSizeNWSE    = TCursor(-8);
  crSizeWE      = TCursor(-9);
  crUpArrow     = TCursor(-10);
  crHourGlass   = TCursor(-11);
  crDrag        = TCursor(-12);
  crNoDrop      = TCursor(-13);
  crHSplit      = TCursor(-14);
  crVSplit      = TCursor(-15);
  crMultiDrag   = TCursor(-16);
  crSQLWait     = TCursor(-17);
  crNo          = TCursor(-18);
  crAppStart    = TCursor(-19);
  crHelp        = TCursor(-20);
  crHandPoint   = TCursor(-21);
  crSizeAll     = TCursor(-22);
}
//其中 crDefault 是默认光标
end;

   
//使用 Screen 更换光标, 是给本程序的全局更换光标, 譬如: begin Screen.Cursor := crSize; end;
//如果让控件有不同的光标, Screen.Cursor 必须是默认值, 也就是: Screen.Cursor := crDefault;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Text := Screen.ActiveControl.ClassName;
end;

{多放几个控件, 按 Tab 测试; 但并不是所有控件都有焦点}

//标题显示当前窗体的类名
procedure TForm1.Timer1Timer(Sender: TObject); begin Text := Screen.ActiveForm.ClassName; end;
//获取输入法列表
begin
  Memo1.Lines := Screen.Imes;
end;

   
//获取当前输入法 var kl: HKL; i: Integer; begin kl := GetKeyboardLayout(0); for i := 0 to Screen.Imes.Count - 1 do if HKL(Screen.Imes.Objects[i]) = kl then ShowMessage(Screen.Imes.Strings[i]); {显示当前输入法} end;





你可能感兴趣的:(Delphi)