我想實現一個非常簡單的方法來通過鼠標選擇屏幕的一個子部分。工作流程是許多應用程序的標準工作流程 - 單擊起點,在第一個點擊鼠標和當前位置之間移動鼠標和透明矩形更新。基本代碼看起來像這樣(減去圖形,這很簡單)mouseUp和mouseMove在應用程序的外部?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" mouseDown="application1_mouseDownHandler(event)">
<fx:Script>
<![CDATA[
import spark.components.mediaClasses.VolumeBar;
private var _anchorPoint:Point = new Point();
private var _currentPoint:Point = new Point();
protected function application1_mouseDownHandler(event:MouseEvent):void
{
addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
_anchorPoint = new Point(event.stageX, event.stageY);
}
private function handleMouseMove(e:MouseEvent):void
{
_currentPoint.x = e.stageX;
_currentPoint.y = e.stageY;
trace("rectangle between (",_anchorPoint.x, ",", _anchorPoint.y, ") and (", _currentPoint.x, ",", _currentPoint.y, ").");
}
private function handleMouseUp(e:MouseEvent):void
{
removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
}
]]>
</fx:Script>
</s:Application>
當用戶將鼠標移動到應用程序外部時,會發生這種情況。不僅_currentPoint停止更新,而且如果您在應用程序外部放開鼠標按鈕,則會錯過mouseUp事件,即將鼠標移回應用程序時_currentPoint將再次開始更新,就像您從未放開鼠標按鈕一樣。想知道是否有Flex(對於web應用程序)通過在應用程序外部(如果可能的話)偵聽mouseMove和mouseUp事件或其他任何可能有意義的方式來解決此問題。
謝謝你的幫助!