Flex 中鼠标在屏幕上的位置

发布于 2024-07-22 05:18:34 字数 1316 浏览 5 评论 0原文

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

我已经尝试了各种方法,目前最接近的是:

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>

I am trying to obtain the actual mouse co-ordinates on the screen so I can create a Native Window at that position but I dont seem to be able to find the right way to do this correctly.

I have tried various things, the closest thing I have at the moment is:

this.contentMouseX and this.contentMouseY

This gives me the coords on the current stage which is fine, then I add to that the:

NativeApplication.nativeApplication.activeWindow.x and activeWindow.y

Which is close, but this doesnt take into account the application title bar.

There must be an easier and more straightforward way of doing this I am sure, can anyone give advice cos I fail to find it on google?

I have tried localToGlobal which doesnt work, it seems that 'global' means within the application and not global to the screen which is of no use to me. Here is an example that shows the failure...

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-07-29 05:18:34

错误启动后...抱歉...

您可以使用 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>

After a false start... Sorry about that...

You can use stage.nativeWindow.globalToScreen - this will do what you need.

In terms of position of the window (for completeness) - you can also look into stage.nativeWindow.x and stage.nativeWindow.y --- this will give you the position for the application on the desktop - from there - you can position relative to that point.

<?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>
清风无影 2024-07-29 05:18:34
//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;
//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;
·深蓝 2024-07-29 05:18:34

这是否有效:

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

Does this work:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文