我正在开发一款Wpf应用程序,我创建了一个眼睛形状的控件,在Canvas中放置了一个椭圆(眼睛),我的目的是当鼠标指针进入Canvas时,椭圆跟随鼠标指针移动。
您有什么建议如何完成这个任务吗?
非常感谢您的关注。
祝好!
编辑:
我已经在Xaml中更新了我的代码:
现在我的目的是当我在Canvas区域(LayoutRoot)移动鼠标时,椭圆形(ctrCircle)只能在Border(border_eye)内移动,并且不能超出"border_eye"的区域。这种效果类似于眼睛。
你有什么想法可以帮助解决这个问题吗?
非常感谢。
祝您有愉快的一天。
干杯!
祝好!
编辑:
我已经在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();
}
现在我的目的是当我在Canvas区域(LayoutRoot)移动鼠标时,椭圆形(ctrCircle)只能在Border(border_eye)内移动,并且不能超出"border_eye"的区域。这种效果类似于眼睛。
你有什么想法可以帮助解决这个问题吗?
非常感谢。
祝您有愉快的一天。
干杯!