开发者

Flex: How can I pop up a component in the center of the user's viewable area?

开发者 https://www.devze.com 2023-02-10 23:22 出处:网络
Let\'s assume I have an application whose dimensions are larger than the user\'s screen resolution. Typically, when adding a popup I would call popupManager.addPopup() followed by popupManager.centerP

Let's assume I have an application whose dimensions are larger than the user's screen resolution. Typically, when adding a popup I would call popupManager.addPopup() followed by popupManager.centerPopup(), but this could cause the popup to be added outside of the user's visible area on screen.

Is there a way to add a popup in the middle of the user's current viewable area instead?

EDIT: Here's a quick example:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               width="3000" height="2000" 
               creationComplete="creationCompleteHandler(event)">

  <fx:Script>
    <![CDATA[

      import mx.managers.PopUpManager;          
      import spark.components.TitleWindow;

      public var myWindow:TitleWindow = new TitleWindow;

      protected function creationCompleteHandler(event:FlexEvent):void
      {
        myWindow.title = "My Title Window";
        PopUpManager.addPopUp(myWindow, DisplayObject(FlexGlobals.topLevelApplication), false);
        PopUpManager.centerPopUp(myWindow);
      }

    ]]>
  </fx:Script>

  <s:BorderContainer width="3000" height="2000" backgroundColor="0x000000" />
</s:Application>

Example 1:

+==========================+---------------------+
|      Viewable Area       |                     |
|                          |                     |
|     +-------------+      |                     |
|     |  I want the |      |                     |
|     |   popup to  |      |                     |
|     |   go here.  |      |                     |
|     +-------------+      |                     |
|                          |                     |
|                          |                     |
+==========================+                     |
|                                                |
|                Rest of Application             |
|                                                |
|                                                |
|                                                |
|                                                |
+------------------------------------------------+

Example 2:

+------------------------------------------------+
|                                                |
|                Rest of Application             |
|                                                |
|                                                |
|                +===========================+   |
|                |       Viewable Area       |   |
|                |         开发者_Go百科                  |   |
|                |      +-------------+      |   |
|                |      |  I want the |      |   |
|                |      |   popup to  |      |   |
|                |      |   go here.  |      |   |
|                |      +-------------+      |   |
|                |                           |   |
|                |                           |   |
|                +===========================+   |
|                                                |
+------------------------------------------------+


centerPopUp() uses the parent object that is passed to addPopUp() or createPopUp() as reference for centering the pop-up. If parent is the application, then it should already be centered based on the width and height of the application - which is what the "viewable area" is from Flex's point of view.

So try passing the application object as the parent argument to addPopUp() or createPopUp()


You can calculate it by hand using the stageWidth and stageHeight properties of the stage property:

dlg.x = (this.stage.stageWidth / 2) - (dlg.width / 2);
dlg.y = (this.stage.stageHeight / 2) - (dlg.height / 2);

There may be a more "correct" way to get the dimensions of the viewable area, but I've found that this gets the job done.

Hope that helps.

EDIT: Fixed my mis-information :)

0

精彩评论

暂无评论...
验证码 换一张
取 消