Flex TitleWindow 拖动范围控制
关于Flex的TitleWindow拖动,可能拖出主application容器,导致titlewindow无法关闭,只能重新加载application。这个问题Adobe在titleWindow的拖动事件里竟然没有处理,这个算是个bug吧。为什么Adobe一直没有重视!
flex 3中我在网上看到的,是下面这种方式解决的,大家可以试试。
本文参考[url]http://www.cnblogs.com/alala666888/archive/2011/05/05/2038206.htmlFlex Air[/url]
在flex里面TitleWindow的拖动范围默认是不控制的,但是我们经常需要将其约束在整个应用程序的主窗口内,我找了半天也没有找到相关属性设置,只能重写,下面是重写的代码:
public class MyTitleWindow extends TitleWindow{//拖动相关private var regX:Number;private var regY:Number;public function MyTitleWindow(){super();}override protected function startDragging(event:MouseEvent):void{regX = event.stageX - x;regY = event.stageY - y; var sbRoot:DisplayObject = systemManager.getSandboxRoot();sbRoot.addEventListener(MouseEvent.MOUSE_MOVE, systemManager_mouseMoveHandler, true); sbRoot.addEventListener(MouseEvent.MOUSE_UP, systemManager_mouseUpHandler, true); sbRoot.addEventListener(SandboxMouseEvent.MOUSE_UP_SOMEWHERE, stage_mouseLeaveHandler); // add the mouse shield so we can drag over untrusted applications.systemManager.deployMouseShields(true);} protected override function stopDragging():void{var sbRoot:DisplayObject = systemManager.getSandboxRoot();sbRoot.removeEventListener(MouseEvent.MOUSE_MOVE, systemManager_mouseMoveHandler, true); sbRoot.removeEventListener(MouseEvent.MOUSE_UP, systemManager_mouseUpHandler, true); sbRoot.removeEventListener(SandboxMouseEvent.MOUSE_UP_SOMEWHERE, stage_mouseLeaveHandler); regX = NaN;regY = NaN; systemManager.deployMouseShields(false);} private function systemManager_mouseMoveHandler(event:MouseEvent):void{ event.stopImmediatePropagation(); if (isNaN(regX) || isNaN(regY)){ return;} var x:Number = event.stageX - regX;var y:Number = event.stageY - regY;var maxX:Number = this.stage.nativeWindow.width - this.width;var maxY:Number = this.stage.nativeWindow.height - this.height; //超出范围 ,停止移动if(x < 0) x = 0;if(y < 0) y = 0;if(x > maxX) x = maxX;if(y > maxY) y = maxY; move(x, y);} private function systemManager_mouseUpHandler(event:MouseEvent):void{// trace("systemManager_mouseUpHandler: " + event);if (!isNaN(regX))stopDragging();} private function stage_mouseLeaveHandler(event:Event):void{// trace("stage_mouseLeaveHandler: " + event);if (!isNaN(regX))stopDragging();}}
override protected function moveArea_mouseDownHandler(event:MouseEvent):void{startDrag(false, new Rectangle(0, 0, this.parentApplication.width - this.width, this.parentApplication.height - this.height));super.moveArea_mouseDownHandler(event);}override protected function moveArea_mouseUpHandler(event:Event):void{stopDrag();super.moveArea_mouseUpHandler(event);}