Flex Air TitleWindow 拖动范围控制

    在flex里面TitleWindow的拖动范围默认是不控制的,但是我们经常需要将其约束在整个应用程序的主窗口内,我找了半天也没有找到相关属性设置,只能重写,下面是重写的代码:

    

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

		}

		

		//拖动相关

		private var regX:Number;

		private var regY:Number;

你可能感兴趣的:(window)