首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex TitleWindow 拖动范畴控制

2012-06-30 
Flex TitleWindow 拖动范围控制关于Flex的TitleWindow拖动,可能拖出主application容器,导致titlewindow无

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();}}

   
需要用到TitleWindow时,就用MyTitleWindow代替原来的TitleWindow。或者不用写上面那个MyTitleWindow控件,直接在自己定义的<mx:TitleWindow>里面重写这两个方法。就ok了。
flex 4的解决方法是这样的:
我们系统是Flex4,TitleWindow的这个问题我是这样解决的,在TitleWindow里加入如下代码(重写titlewindow的拖动方法,包括开始拖动和结束拖动,在拖动时添加范围限制),
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);}

热点排行