Posts tagged ‘Window’

Create popup window and center on the second screen

Sometimes you need a way to create popup window from your application(WindowedApplication). In that case you could use code from my last post Center AIR Window on the screen. This code works just fine for windows(WindowedApplication and Window) running on the main screen. It wouldn’t work correctly in a case where main application(WindowedApplication) is running NOT on the main screen but on the second, third etc monitor. Below picture shows you just that, WindowedApplication is running on the second screen and popup window is center vertically and horizontally above it.

Jump into code

Center AIR Window on the screen

You might have noticed that Window or WindowedApplication doesn’t load in the center of your screen. By default, window loads somewhere in the left side on your desktop. I personally think, that this is a bad user experience and you should always center window vertically and horizontally on screen. You can do this by setting window x coordinate to (screen.width – window.width)/2 ¬†and y coordinate to (screen.height – window.height)/2.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="centerWindow(this)" viewSourceURL="srcview/index.html"> 

    <mx:Script>
        <![CDATA[
            import mx.core.Window;

            private function centerWindow(window:WindowedApplication):void
            {
                var screenBounds:Rectangle = Screen.mainScreen.bounds;
                var nativeWindow:NativeWindow  = window.nativeWindow;

                nativeWindow.x = (screenBounds.width - nativeWindow.width)/2;
                nativeWindow.y = (screenBounds.height - nativeWindow.height)/2;
            }
        ]]>
    </mx:Script> 

</mx:WindowedApplication>