弹性屏幕上的鼠标位置

时间:2009-05-29 07:30:23

标签: actionscript-3 flex flex3

我试图在屏幕上获得实际的鼠标坐标,这样我就可以在该位置创建一个原生窗口,但我似乎无法找到正确的方法来正确执行此操作。

我尝试了各种各样的东西,目前我最接近的是:

this.contentMouseX and this.contentMouseY

这给了我当前阶段的协调,这很好,然后我补充说:

NativeApplication.nativeApplication.activeWindow.x and activeWindow.y

哪个很接近,但这并没有考虑应用程序标题栏。

必须有一种更简单,更直接的方式来做到这一点我相信,任何人都可以提供建议,因为我无法在谷歌上找到它吗?

我尝试过localToGlobal,它不起作用,似乎'全局'意味着在应用程序内,而不是全局到屏幕,这对我没用。这是一个显示失败的例子......

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.core.Application;

            private function click(evt:MouseEvent):void
            {
                var pt:Point = new Point( this.contentMouseX, this.contentMouseY );
                var global:Point = Application.application.localToGlobal( pt );

                trace( "local_x: " + pt.x + " x " + pt.y );
                trace( "global_x: " + global.x + " x " + global.y );
            }
        ]]>
    </mx:Script>

    <mx:HBox horizontalAlign="center" width="100%">
        <mx:Button id="butt" label="Click" click="click(event)" />
    </mx:HBox>
</mx:WindowedApplication>

3 个答案:

答案 0 :(得分:5)

错误的开始之后......对不起......

您可以使用stage.nativeWindow.globalToScreen - 这将满足您的需求。

就窗口的位置而言(为了完整性) - 您还可以查看stage.nativeWindow.x和stage.nativeWindow.y ---这将为您提供桌面上应用程序的位置 - 从那里 - 你可以相对于那一点定位。

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.core.Application;

private function click(evt:MouseEvent):void
{
  var pt:Point = new Point( this.butt.x, this.butt.y );
  var global:Point = Application.application.localToGlobal( pt );

  trace( "local_x: " + pt.x + " x " + pt.y );
  trace( "global_x: " + global.x + " x " + global.y );

  var p:Point = stage.nativeWindow.globalToScreen(new Point(this.butt.x, this.butt.y));

  trace(p.x + " x " + p.y);
  var na:NativeWindow = new NativeWindow(new NativeWindowInitOptions());
  na.visible = true;
  na.width = 100;
  na.height = 100;
  na.x = p.x;
  na.y = p.y;
  na.activate();

}
]]>
</mx:Script>


<mx:Button x="10" y="10" id="butt" label="Click" click="click(event)" />

</mx:WindowedApplication>

答案 1 :(得分:0)

//get the application level mouse coordinate
var local_x : int = this.mouseX; 

//convert this x value into a global coordinate
var pt : Point = new Point(local_x, 0);
var global_x : int = this.localToGlobal(pt).mouseX;

答案 2 :(得分:0)

这是否有效:

private function click(evt:MouseEvent):void
{
  var pt:Point = new Point( evt.localX, evt.localY );
  var global:Point = new Point( evt.stageX, evt.stageY);

  trace( "local_x: " + pt.x + " x " + pt.y );
  trace( "global_x: " + global.x + " x " + global.y );
}