Flex中如何通过监听Object对象mouseLeave事件检测鼠标是否进入/离开Flex应用的例子

By Minidxer | April 14, 2009

接下来的例子演示了Flex中如何通过监听Object对象mouseLeave事件,检测鼠标是否进入/离开Flex应用。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="top"
  5.         backgroundColor="white"
  6.         applicationComplete="init();">
  7.  
  8.     <mx:Style>
  9.         Alert {
  10.             modalTransparencyColor: black;
  11.             modalTransparency: 0.6;
  12.         }
  13.     </mx:Style>
  14.  
  15.     <mx:Script>
  16.         <![CDATA[
  17.             import mx.controls.Alert;
  18.             import mx.managers.PopUpManager;
  19.  
  20.             private var _isMouseInSWF:Boolean = false;
  21.             private var alert:Alert;
  22.  
  23.             [Bindable]
  24.             private function get isMouseInSWF():Boolean {
  25.                 return _isMouseInSWF;
  26.             }
  27.  
  28.             private function set isMouseInSWF(value:Boolean):void {
  29.                 _isMouseInSWF = value;
  30.             }
  31.  
  32.             private function init():void {
  33.                 stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_mouseMove);
  34.                 stage.addEventListener(Event.MOUSE_LEAVE, stage_mouseLeave);
  35.             }
  36.  
  37.             private function stage_mouseLeave(evt:Event):void {
  38.                 isMouseInSWF = false;
  39.                 if (alert) {
  40.                     PopUpManager.removePopUp(alert);
  41.                 }
  42.                 alert = Alert.show("GONE");
  43.             }
  44.  
  45.             private function stage_mouseMove(evt:MouseEvent):void {
  46.                 if (!isMouseInSWF) {
  47.                     if (alert) {
  48.                         PopUpManager.removePopUp(alert);
  49.                     }
  50.                     // alert = Alert.show("BACK");
  51.                     isMouseInSWF = true;
  52.                 }
  53.             }
  54.         ]]>
  55.     </mx:Script>
  56.  
  57.     <mx:Label text="isMouseInSWF: {isMouseInSWF}" />
  58.  
  59. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Other, PopUpManager | No Comments » | Tags: , , , , ,

Search Posts