对象跟随鼠标光标

2024-04-01

我正在做一个Wpf应用程序,我创建了一个眼睛形状的控件,我在画布中放置了一个椭圆(眼睛),我的目的是当鼠标光标进入画布时,椭圆跟随鼠标光标。 您对如何执行此任务有什么建议吗? 非常感谢您的关注。

Cheers

EDIT

我已经更新了 Xaml 中的代码:

<Window Height="480" Title="Window2" Width="640" x:Class="WpfApplication5.Window2"
   x:Name="Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
    <Storyboard x:Key="OnLoaded1">
      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ctrCircle" 
        Storyboard.TargetProperty="(UIElement.RenderTransform).( TransformGroup.Children)[3].(TranslateTransform.X)">
        <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1">
          <EasingDoubleKeyFrame.EasingFunction>
            <ExponentialEase EasingMode="EaseOut" />
          </EasingDoubleKeyFrame.EasingFunction>
      </EasingDoubleKeyFrame>
      </DoubleAnimationUsingKeyFrames>
      <DoubleAnimationUsingKeyFrames Storyboard.TargetName="ctrCircle" 
          Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
        <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1">
        <EasingDoubleKeyFrame.EasingFunction>
            <ExponentialEase EasingMode="EaseOut" />
          </EasingDoubleKeyFrame.EasingFunction>
        </EasingDoubleKeyFrame>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Style TargetType="Ellipse">
      <Setter Property="RenderTransform">
        <Setter.Value>
          <ScaleTransform ScaleX="1" ScaleY="1"/>
        </Setter.Value>
      </Setter>
      <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    </Style>
  </Window.Resources>

  <Canvas MouseMove="mov" x:Name="LayoutRoot">
    <Border ackground="Black" B="" Canvas.Left="178" Canvas.Top="103" 
      CornerRadius="250" Height="255.5" Width="290" x:Name="border_eye">
      <Ellipse Fill="#FFFFC600" Height="12" HorizontalAlignment="Left" 
        Margin="0" RenderTransformOrigin="0.5,0.5" Stroke="{x:Null}" 
        VerticalAlignment="Center" Visibility="Visible" Width="12" x:Name="ctrCircle">
        <Ellipse.RenderTransform>
          <TransformGroup>
            <ScaleTransform />
            <SkewTransform />
            <RotateTransform />
            <TranslateTransform />                 
          </TransformGroup>
        </Ellipse.RenderTransform>
      </Ellipse>
    </Border>
  </Canvas>
</Window>

并在后面的代码中:

private void mov(object sender, MouseEventArgs e)
    {
 System.Windows.Point pt = e.GetPosition((Canvas)sender);
      Storyboard invokeStoryboard = this.Resources["OnLoaded1"] as Storyboard;
        ((DoubleAnimationUsingKeyFrames)invokeStoryboard.Children[0]).KeyFrames[0].Value = pt.X;
        ((DoubleAnimationUsingKeyFrames)invokeStoryboard.Children[1]).KeyFrames[0].Value = pt.Y;
        invokeStoryboard.Begin();
    }

现在我的目的是,当我在画布区域(LayoutRoot)中移动鼠标时,椭圆(ctrCircle)仅在边框(border_eye)内移动,并且不克服“border_eye”的区域,这种效果类似于眼睛。

您有任何意见来完成此步骤吗?

非常感谢

祝你今天过得愉快。

Cheers


以下是如何使用 WPF 画布中的眼睛的示例接收框架 http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx。使用 Rx 而不是直接附加到鼠标移动事件,您可以缓冲事件并仅每 10 毫秒更新一次瞳孔位置,从而减少总体 CPU 负载。

The Xaml

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="namespace.EyeDemo"
    x:Name="UserControl"
    d:DesignWidth="640" d:DesignHeight="480">

    <Canvas x:Name="LayoutRoot" Background="GreenYellow">
        <Ellipse Fill="Black" Width="120" Height="70" Canvas.Left="90" Canvas.Top="115"/>
        <Ellipse x:Name="Eye" Fill="Black" Width="100" Height="50" Canvas.Left="100" Canvas.Top="125"/>
        <Ellipse x:Name="Pupil" Fill="Red" Height="20" Canvas.Left="139" Canvas.Top="138" Width="20"/>
    </Canvas>
</UserControl>

以及后面的代码

/// <summary>
/// Interaction logic for EyeDemo.xaml
/// </summary>
public partial class EyeDemo : UserControl
{
    public EyeDemo()
    {
        this.InitializeComponent();

        double majorRadius = Eye.Width / 2d;
        double minorRadius = Eye.Height / 2d;
        Point center = new Point( Canvas.GetLeft( Eye ) + majorRadius, Canvas.GetTop( Eye ) + minorRadius );

        // create event streams for mouse down/up/move using reflection
        // to keep taking mouse move events and return the X, Y positions
        var mouseMove = from evt in Observable.FromEvent<MouseEventArgs>( LayoutRoot, "PreviewMouseMove" )
                        select (Point?)evt.EventArgs.GetPosition( this );

        // subscribe to the stream of position changes and modify the Canvas.Left and Canvas.Top
        // use the bound by elipse function to restrain the pupil to with the eye.
        mouseMove.BufferWithTime( TimeSpan.FromMilliseconds( 10 ) ).Select( p => BoundByElipse( majorRadius, minorRadius, center, p.LastOrDefault() ) )
            .ObserveOnDispatcher().Subscribe( pos =>
              {
                  if( pos.HasValue )
                  {
                      Canvas.SetLeft( Pupil, pos.Value.X - Pupil.Width / 2d );
                      Canvas.SetTop( Pupil, pos.Value.Y - Pupil.Height / 2d );
                  }
              } );
    }

    private Point? BoundByElipse( double majorRadius, double minorRadius, Point center, Point? point )
    {
        if( point.HasValue )
        {
            // Formular for an elipse is  x^2 / a^2 + y^2 / b^2 = 1
            // where a = majorRadius and b = minorRadius
            // Using this formular we can work out if the point is with in the elipse 
            // or find the boundry point closest to the point

            // Find the location of the point relative to the center.
            Point p = new Point( point.Value.X - center.X, point.Value.Y - center.Y );
            double a = majorRadius;
            double b = minorRadius;

            double f = p.X * p.X / (a * a) + p.Y * p.Y / (b * b);
            if( f <= 1 )
            {
                // the point is with in the elipse;
                return point;
            }
            else
            {
                // the point is outside the elipse, therefore need to find the closest location on the boundry.
                double xdirection = point.Value.X > center.X ? 1 : -1;
                double ydirection = point.Value.X > center.X ? 1 : -1;

                double r = p.X / p.Y;

                double x = p.Y != 0 ? Math.Sqrt( r * r * a * a * b * b / (r * r * b * b + a * a) ) : a;
                double y = r != 0 ? x / r : (point.Value.Y > center.Y ? -b : b);

                return new Point( center.X + xdirection * x, center.Y + ydirection * y );
            }
        }
        else
        {
            return null;
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对象跟随鼠标光标 的相关文章

随机推荐

  • 前向声明会产生不兼容的类型错误

    我一直在阅读一些有关前瞻性声明的文章 包括在这个论坛上 他们都说它可以让我们免于包含头文件 但是以下代码会生成错误 ifndef CLASSA H define CLASSA H class B class A public A A int
  • 有没有办法在 protobuf (proto2) 中创建类型别名?

    是否可以创建 protobuf 标量类型的别名 例如 我想使用Sequence替代string 即使它们是二进制等价的 我的近期目标是使文档 使用 protoc gen doc 生成 更容易理解 理想情况下 这种类型应该用支持类型检查的语言
  • Postgresql:无法访问文件“$libdir/postgis-2.1”没有这样的文件或目录

    我想这是一个已知问题 但由于我使用了删除 postgresql 9 4 postgis 2 1 的脚本 所以我现在无法在 Debian 下消除此 SQL 错误 无法访问文件 libdir postgis 2 1 没有这样的文件或目录 我做了
  • 当我尝试转换我的应用程序时,DesktopAppConverter (DAC) 表示扩展的图像无效

    我已经下载了与我的 Windows 版本 14393 匹配的 wim 文件 并完成了设置转换器的过程 所以 我的机器上运行了转换器应用程序 当我尝试运行转换时 收到错误消息 E EXPANDED BASEIMG INVALID 我尝试通过使
  • Git子树合并策略,可以不合并历史记录吗?

    我一直在尝试摆脱子模块以获得独立的存储库 并且子树合并策略 http help github com subtree merge 似乎符合这个用例 然而 合并的存储库的历史出现在我自己的项目的历史中 这相当烦人 我试过了git filter
  • Vue.js 依赖选择

    我正处于学习 Vue js 的初级阶段 遇到了我现在无法解决的问题 所以我有 1 个选择字段 data list Option 1 size 1 prize 5 size 2 prize 10 然后我像这样填充第一个选择字段
  • WAMPServer 3.2.0:无法访问该站点

    我一直在使用 WAMPServer 来运行 PHP 并且为此使用端口 8181 今天 突然 我无法使用 WAMPServer 访问 localhost 当我尝试打开以下地址的页面时收到此消息 http localhost 8181 无法访问
  • 为什么 Rails 找不到我的资产?

    在生产模式下 rails 似乎无法从资产管道中找到任何预编译资产 我使用的是在 CentOS 上的 RVM 内运行的 Rails 3 2 0 和 ruby 1 9 3 没有其他 Web 服务器与此应用程序一起运行 该应用程序最近才更新为使用
  • 如何在react-dnd-treeview库上使用Selenium测试拖放

    我正在尝试在 React 应用程序上测试简单的拖放行为 我正在使用反应 dnd 树视图 https github com teleport react dnd treeview图书馆和他们的示例网站 http teleport github
  • PHP - 自我表单提交:$_SERVER['PHP_SELF'] OR action=""?

    我只是意识到 在一些奇怪的情况下 我正在做我认为是自我提交的事情 而没有在操作表单属性上引用 PHP SELF 我很困惑 我们可以使用 Or action 如果不是 在什么情况下我们应该考虑其中一种或另一种 提前致谢 模模 您可以使用 PH
  • d3 力图:粘性节点

    我想在 D3 中的力导向图形布局中添加一种行为 一旦放下 拖放的 svg 节点就会粘在其位置上 无论图形中发生什么情况 都不再改变位置 我已经阅读了一些有关此 API 的内容 但我无法找到一种方法来使其正常工作 我试图解决的问题是允许用户
  • 使用 dplyr 进行汇总并保持相同的变量名

    我发现 data table 和 dplyr 在尝试做同样的事情时有不同的结果 我想使用 dplyr 语法 但让它以 data table 的方式进行计算 用例是我想将小计添加到表中 为此 我需要对每个变量进行一些聚合 但然后保留相同的变量
  • 如何从 bash 执行 mongo 命令?

    我正在尝试从 bash 脚本运行此命令 mongo 192 168 10 20 27000 eval use admin db shutdownServer quit 但我收到此错误 rs initiate use admin db shu
  • 从选择的结果创建插入脚本

    使用 SQL Server Management Studio 有一种方法可以在选择结果网格中选择一行或多行 并让 SQL Server Mangement Studio 生成一个或多个插入语句 每选择一行一个 该语句会将该数据插入到表中相
  • libuv 只是 POSIX 系统上 libev 的包装吗?

    我真的很困惑 libev 和 libuv libuv 只是 POSIX 系统上 libev 的包装吗 如果不是的话区别在哪里 不再 从 libuv v0 9 开始 这是libuv github问题 https github com joye
  • 访问 VBA:如何获取表单中的输入并在查询中使用它们

    HI 我有一个带有几个按钮的表单 每个按钮运行几行代码 代码中也有查询 例如 select from table where number 6 现在 如果我想在表单中输入数字 我该怎么做 在按钮附近添加一个未绑定的文本控件 更新您的按钮代码
  • 如何在 R 中使用神经网络包时实现自己的误差函数?

    我正在尝试在 R 中的神经网络包中实现自定义错误函数 通常使用代表误差平方和和交叉熵的 sse 和 ce 来计算误差 任何人都可以向我提供有关如何实现自己的误差函数的详细信息 虽然软件包说我们可以使用自定义的错误函数 但用户手册中没有对此提
  • 有没有办法判断 Safari 是否全屏? (例如 document.fullscreenElement)

    我正在尝试使用 Javascript 确定 Safari 是否处于全屏模式 Chrome 和 Mozilla 都使用带有供应商前缀的 document fullscreenElement 版本 isFullscreen function r
  • 单击 Firebase 动态链接后 - DynamicLinks.dynamicLinks().handleUniversalLink 返回 false

    我正在使用 Firebase 动态链接 并且在 Firebase 控制台中创建的链接很少 这是我复制粘贴到电子邮件并单击的示例短链接 https app xxx com deeplink xyz https app xxx com deep
  • 对象跟随鼠标光标

    我正在做一个Wpf应用程序 我创建了一个眼睛形状的控件 我在画布中放置了一个椭圆 眼睛 我的目的是当鼠标光标进入画布时 椭圆跟随鼠标光标 您对如何执行此任务有什么建议吗 非常感谢您的关注 Cheers EDIT 我已经更新了 Xaml 中的