General experience of building applications with Flash Professional is suggested. For more details on getting started with this Quick Start, refer to Building the Quick Start sample applications with Flash.
Intermediate
The PixelPerfect sample application, shown in Figure 1, performs basic tasks on window objects. These tasks include creating, resizing, moving, and closing windows.
The PixelPerfect application demonstrates how you can use the AIR window API to:
startResize() method of the NativeWindow class to resize a windowstartMove() method of the NativeWindow class to move a windowclose() method of the NativeWindow class to close a windowNote: This is a sample application provided, as is, for instructional purposes.
PixelPerfect presents a simple user interface that lets users access the NativeWindow API to modify application windows based on user input. PixelPerfect supports the following user interactions:
mouseDown event triggers application logic to run that calculates the mouse position and, if appropriate, makes a call to the NativeWindow.startResize() method. mouseDown event triggers application logic that calculates the mouse position and, if appropriate, makes a call to the NativeWindow.startMove() method. keyDown event triggers application logic that detects if an arrow key was pressed. If it was, the NativeWindow.x or NativeWindow.y property is either increased or decreased by 1, depending on whether the arrow that was pressed was the Down arrow key, the Up arrow key, the Left arrow key, or the Right arrow key. This process occurs each time the user presses an arrow key. keyDown event triggers application logic that detects if the Shift key was pressed in combination with an arrow key. If such a key combination was pressed, the NativeWindow.width or Window.height property is either increased or decreased by 1, depending on whether the arrow that was pressed was the Down arrow key, the Up arrow key, the Left arrow key, or the Right arrow key. This process occurs each time the user presses the Shift key in combination with an arrow key. doubleClick event triggers application logic that makes an explicit call to the Window.close() method. keyDown event triggers application logic that detects if the Ctrl+N keys was pressed. If so, a new instance of the Ruler class (which extends NativeWindow ) is created, displaying a new ruler window. To create a window, PixelPerfect instantiates a new Ruler object. The Ruler class extends the NativeWindow class. In the Ruler constructor, the NativeWindowInitOptions object is created and passed to the NativeWindow super class constructor:
public function Ruler(width:uint = 300, height:uint = 300,
x:uint = 50, y:uint = 50, alpha:Number = .4)
{
var winArgs:NativeWindowInitOptions = new NativeWindowInitOptions();
winArgs.systemChrome = NativeWindowSystemChrome.NONE;
winArgs.transparent = true;
super(winArgs);
//...
The rest of the constructor initializes the properties of the new window and its members.
The startResize() method receives a constant value defined by the NativeWindowResize class that indicates the edge or corner of the window from which the user is dragging the window to resize it. The startResize() method triggers a system-controlled resizing of the window.
The application also explicitly sets the window bounds in response to keyboard events. In this case, no resize events are involved. The following code snippet sets the window bounds inside a switch statement:
switch (e.keyCode)
{
case Keyboard.DOWN:
if (e.shiftKey)
height += 1;
else
y += 1;
drawTicks(bounds);
break;
case Keyboard.UP:
if (e.shiftKey)
height -= 1;
else
y -= 1;
drawTicks(bounds);
break;
case Keyboard.RIGHT:
if (e.shiftKey)
width += 1;
else
x += 1;
drawTicks(bounds);
break;
case Keyboard.LEFT:
if (e.shiftKey)
width -= 1;
else
x -= 1;
drawTicks(bounds);
break;
case 78:
createNewRuler();
break;
}
This code snippet also shows the key events used to move the window and to create new windows.
The startMove() method is used to begin a system-controlled move of the window. The PixelPerfect sample application uses this method when the mouse device controls the move.
When the arrow keys control the parameters of the move, PixelPerfect changes the window bounds, one pixel at a time, by incrementing or decrementing the x or y property of the window. (The code is included in the switch statement in the preceding code snippet.)
The visible background of the window is a Sprite object that was added to the ruler stage. The application changes the transparency of the background by adjusting the alpha property of the background sprite in response to mouse wheel events:
private function onMouseWheel(e:MouseEvent):void
{
var delta:int = (e.delta < 0) ? -1 : 1;
if (sprite.alpha >= .1 || e.delta > 0)
sprite.alpha += (delta / 50);
}
For a window to be transparent against the desktop, the window must be created with transparency enabled by setting transparent property to true in the NativeWindowsInitOptions object passed to the window constructor. The transparency state of a window cannot be changed once the window is created.
When a doubleclick event is dispatched, PixelPerfect calls the close() method of the window.
private function onDoubleClick(e:Event):void
{
close();
}
A close event is dispatched after the window is closed, signaling the completion of the close process. After a window is closed, it cannot be reopened. Closing the window frees the resources associated with the window (unless they are referenced elsewhere) so that they can be garbage collected. However, the initial window created by AIR is never garbage collected. The application is responsible for closing its windows so that the associated resources can be freed. When the last window of an application is closed, the application also exits. This default behavior can be changed by setting the Shell object autoExit property to false.
For more information about the methods and properties of the AIR windowing API, see the ActionScript 3 Reference for the Flash Platform.