3 Replies Latest reply on Aug 14, 2009 6:25 PM by GA-Sembada

    Center window in screen




      I have a program with a fixed resolution of 940 x 600 px. I would like it to be centered in the screen the first time it opens, no matter wich resolution the screen has.


      I allready found a code for this in the AIR cookbook, but it's for Flex. I would need something that uses JavaScript and HTML.


      Just a property that gives me the screens height and width!



        • 1. Re: Center window in screen
          anirudhs Level 2



          You can get the screen bounds via window.runtime.flash.display.Screen.mainScreen.visibleBounds.

          • 2. Re: Center window in screen

            here's some additional code etc.


            function centerWindow(){


                        //default bounds of nativeWindow
                        var applicationBounds = window.nativeWindow.bounds;


                        //determine which screen we're located on
                        var screens = air.Screen.getScreensForRectangle(window.nativeWindow.bounds);


                        var screenBounds = (screens.length > 0) ? screens[0].visibleBounds : air.Screen.mainScreen.visibleBounds;
                        //get initial position
                        x = (screenBounds.width - applicationBounds.width) / 2;
                        y = (screenBounds.height - applicationBounds.height) / 2;
                        //adjust for offset x or offset y (multi monitors)
                        x = screenBounds.x + x;
                        y = screenBounds.y + y;
                        window.nativeWindow.x = x;
                        window.nativeWindow.y = y;

            • 3. Re: Center window in screen

              Hi Jason,


              It works for me. I put those line of codes and load it using "onload" event in <body>.

              although it works, but i'm finding another method to call the function.

              Is there any other way?


              Thank you for the codes, it helps me a lot.