Screen 类提供此应用程序的可用显示屏幕的相关信息。
屏幕是位于可能更大的“虚拟桌面”内的独立桌面区域。虚拟桌面的原点是操作系统指定的主屏幕的左上角。因此,个别显示屏幕范围的坐标可能是负数。虚拟桌面也可能有一些不在任何显示屏幕内的区域。
Screen 类包括用于访问可用屏幕对象的静态类成员和用于访问个别屏幕属性的实例成员。屏幕信息不应进行缓存,因为这些信息可能会由用户随时更改。
请注意,屏幕和连接到计算机的物理监视器之间不一定有一对一的对应关系。例如,两台监视器可以显示同一个屏幕。
无法直接实例化 Screen 类。调用 new Screen()
构造函数将引发 ArgumentError
异常。
下面的示例通过定义 DockingWindow 类来创建一个停靠在屏幕某一侧的窗口。执行下列步骤可完成该任务:
- 响应键盘事件以确定停靠在屏幕的哪一侧。
- 访问静态 Screen 类方法
getScreensForRectangle()
可以获取当前显示该窗口的屏幕的 Screen 对象。 - 根据屏幕尺寸重新设置窗口范围。
- 根据新窗口尺寸重新绘制窗口内容。
请注意,此类旨在用作具有 SystemChrome="none"
和 transparent="true"
设置的 AIR 应用程序的根类。要在具有系统镶边的窗口中使用此类,在计算窗口位置和大小时,必须将镶边的粗细和窗口的最小宽度考虑在内。
1 package 2 { 3 import flash.display.Screen; 4 import flash.display.Sprite; 5 import flash.display.StageAlign; 6 import flash.display.StageScaleMode; 7 import flash.events.KeyboardEvent; 8 import flash.geom.Rectangle; 9 import flash.ui.Keyboard; 10 11 public class DockingWindow extends Sprite 12 { 13 private const dockedWidth:uint = 80; 14 private const dockedHeight:uint = 80; 15 16 public function DockingWindow():void{ 17 stage.align = StageAlign.TOP_LEFT; 18 stage.scaleMode = StageScaleMode.NO_SCALE; 19 stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey); 20 dockLeft(); 21 } 22 23 private function onKey(event:KeyboardEvent):void{ 24 switch(event.keyCode){ 25 case Keyboard.LEFT : 26 dockLeft(); 27 break; 28 case Keyboard.RIGHT : 29 dockRight(); 30 break; 31 case Keyboard.UP : 32 dockTop(); 33 break; 34 case Keyboard.DOWN : 35 dockBottom(); 36 break; 37 case Keyboard.SPACE : 38 stage.nativeWindow.close(); 39 } 40 } 41 42 public function dockLeft():void{ 43 var screen:Screen = getCurrentScreen(); 44 stage.nativeWindow.x = screen.visibleBounds.left; 45 stage.nativeWindow.y = screen.visibleBounds.top; 46 stage.nativeWindow.height = screen.visibleBounds.height; 47 stage.stageWidth = dockedWidth; 48 drawContent(); 49 } 50 51 public function dockRight():void{ 52 var screen:Screen = getCurrentScreen(); 53 stage.nativeWindow.x = screen.visibleBounds.width - dockedWidth; 54 stage.nativeWindow.y = screen.visibleBounds.top; 55 stage.stageWidth = dockedWidth; 56 stage.nativeWindow.height = screen.visibleBounds.height; 57 drawContent(); 58 } 59 60 public function dockTop():void{ 61 var screen:Screen = getCurrentScreen(); 62 stage.nativeWindow.x = screen.visibleBounds.left; 63 stage.nativeWindow.y = screen.visibleBounds.top; 64 stage.nativeWindow.width = screen.visibleBounds.width; 65 stage.stageHeight = dockedHeight; 66 drawContent(); 67 } 68 69 public function dockBottom():void{ 70 var screen:Screen = getCurrentScreen(); 71 stage.nativeWindow.x = screen.visibleBounds.left; 72 stage.nativeWindow.y = screen.visibleBounds.height - dockedHeight; 73 stage.nativeWindow.width = screen.visibleBounds.width; 74 stage.stageHeight = dockedHeight; 75 drawContent(); 76 } 77 78 private function getCurrentScreen():Screen{ 79 return Screen.getScreensForRectangle(stage.nativeWindow.bounds)[0]; 80 } 81 82 private function drawContent():void{ 83 const size:int = 60; 84 const pad:int = 10; 85 var numHSquares:int = Math.floor(stage.stageWidth/(size + pad)); 86 var numVSquares:int = Math.floor(stage.stageHeight/(size + pad)); 87 with (graphics){ 88 clear(); 89 lineStyle(1); 90 beginFill(0x3462d5,.7); 91 for(var i:int = 0; i < numHSquares; i++){ 92 for(var j:int = 0; j < numVSquares; j++){ 93 drawRect((i * (size + pad)) + pad, (j * (size + pad)) + pad, size, size); 94 } 95 } 96 endFill(); 97 } 98 } 99 } 100 }