flex titlewindow的resize方法一
1、主页面:ResizableTitleWindowApp.mxml
<?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" width="500" height="300" viewSourceURL="srcview/index.html"> <fx:Script> <![CDATA[ import mx.core.IFlexDisplayObject; import mx.managers.PopUpManager; public var count:Number = 0; public function createPopUp():void { var rtw:ResizableTitleWindow = new ResizableTitleWindow(); rtw.title="This is pop up " + count; rtw.height = 100; rtw.width = 170; rtw.x = 50; rtw.y = 10; count++; var st:Label = new Label(); st.text = "This ResizableTitleWindow can be dragged, activated, closed, and resized."; st.top = st.left = st.right = st.bottom = 10; rtw.addEventListener("close", closeHandler); rtw.addElement(st); rtw.setStyle("skinClass", ResizableTitleWindowSkin); PopUpManager.addPopUp(rtw, this, false); } private function closeHandler(event:Event):void { event.target.removeEventListener("close", closeHandler); PopUpManager.removePopUp(event.target as IFlexDisplayObject); } ]]> </fx:Script> <s:layout> <s:VerticalLayout paddingTop="10" horizontalAlign="center"/> </s:layout> <s:Label text="ResizableTitleWindow" fontSize="16" fontWeight="bold" /> <s:Button label="Create Resizable TitleWindow" click="createPopUp();" /></s:WindowedApplication>2、titlewindow的as文件(ResizableTitleWindow.as):package{ import flash.display.DisplayObject; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import mx.core.UIComponent; import mx.events.SandboxMouseEvent; import spark.components.TitleWindow; /** * ResizableTitleWindow is a TitleWindow with * a resize handle. */ public class ResizableTitleWindow extends TitleWindow { //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Constructor. */ public function ResizableTitleWindow() { super(); } //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- private var clickOffset:Point; //-------------------------------------------------------------------------- // // Properties // //-------------------------------------------------------------------------- //---------------------------------- // Resize Handle //---------------------------------- [SkinPart("false")] /** * The skin part that defines the area where * the user may drag to resize the window. */ public var resizeHandle:UIComponent; //-------------------------------------------------------------------------- // // Overridden methods: UIComponent, SkinnableComponent // //-------------------------------------------------------------------------- /** * @private */ override protected function partAdded(partName:String, instance:Object) : void { super.partAdded(partName, instance); if (instance == resizeHandle) { resizeHandle.addEventListener(MouseEvent.MOUSE_DOWN, resizeHandle_mouseDownHandler); } } /** * @private */ override protected function partRemoved(partName:String, instance:Object):void { if (instance == resizeHandle) { resizeHandle.removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandle_mouseDownHandler); } super.partRemoved(partName, instance); } //-------------------------------------------------------------------------- // // Event Handlers // //-------------------------------------------------------------------------- private var prevWidth:Number; private var prevHeight:Number; protected function resizeHandle_mouseDownHandler(event:MouseEvent):void { if (enabled && isPopUp && !clickOffset) { clickOffset = new Point(event.stageX, event.stageY); prevWidth = width; prevHeight = height; var sbRoot:DisplayObject = systemManager.getSandboxRoot(); sbRoot.addEventListener( MouseEvent.MOUSE_MOVE, resizeHandle_mouseMoveHandler, true); sbRoot.addEventListener( MouseEvent.MOUSE_UP, resizeHandle_mouseUpHandler, true); sbRoot.addEventListener( SandboxMouseEvent.MOUSE_UP_SOMEWHERE, resizeHandle_mouseUpHandler) } } /** * @private */ protected function resizeHandle_mouseMoveHandler(event:MouseEvent):void { // during a resize, only the TitleWindow should get mouse move events // we don't check the target since this is on the systemManager and the target // changes a lot -- but this listener only exists during a resize. event.stopImmediatePropagation(); if (!clickOffset) { return; } width = prevWidth + (event.stageX - clickOffset.x); height = prevHeight + (event.stageY - clickOffset.y); event.updateAfterEvent(); } /** * @private */ protected function resizeHandle_mouseUpHandler(event:Event):void { clickOffset = null; prevWidth = NaN; prevHeight = NaN; var sbRoot:DisplayObject = systemManager.getSandboxRoot(); sbRoot.removeEventListener( MouseEvent.MOUSE_MOVE, resizeHandle_mouseMoveHandler, true); sbRoot.removeEventListener( MouseEvent.MOUSE_UP, resizeHandle_mouseUpHandler, true); sbRoot.removeEventListener( SandboxMouseEvent.MOUSE_UP_SOMEWHERE, resizeHandle_mouseUpHandler); } }}