繁体   English   中英

用鼠标移动图像wpf c#

[英]Move image with mouse wpf c#

您好,我一直试图解决这个问题一段时间了,我希望您能够引导我朝着正确的方向前进!

我的目标是在我的wpf游戏中有一个“手电筒”效果,为此我已经追求了2个主要选项。

  1. 有一个200%宽度和高度的黑色png图像,中心有一个孔,然后用鼠标移动图像
  2. 具有与窗口大小相同的黑色图像,并且具有不透明蒙版元素,该元素是圆形,通过黑色层看到下面的内容。 这个圆圈随着鼠标移动。

我无法使用这些选项中的任何一个,在wpf中,当你有一个不透明蒙版时,你看起来无法移动“穿透”该层的元素。 而对于大图像方法,我无法将鼠标Point对象转换为图像的位置。 下面是一些获取鼠标位置的潜在方法,我无法将图像移动到“点”位置。

指向屏幕

private void MouseCordinateMethod(object sender, MouseEventArgs e)
{
    var relativePosition = e.GetPosition(this);
    var point= PointToScreen(relativePosition);
    _x.HorizontalOffset = point.X;
    _x.VerticalOffset = point.Y;
}

为getPosition

var point = e.GetPosition(this.YourControl);

最终的游戏将使用kinect进行控制,因此如果有一种方法可以使用kinect库或本机,这也是一种选择。 谢谢。

您可以使用具有CombinedGeometry的Path元素,该CombinedGeometry包含非常大的RectangleGeometry和排除(因此透明)的EllipseGeometry ,通过在鼠标输入上更改其Center属性来移动它:

<Grid MouseMove="Grid_MouseMove">
    <TextBlock Text="Hello, World" FontSize="40"
               HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Path Fill="Black">
        <Path.Data>
            <CombinedGeometry GeometryCombineMode="Exclude">
                <CombinedGeometry.Geometry1>
                    <RectangleGeometry Rect="0,0,10000,10000"/>
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                    <EllipseGeometry x:Name="circle" RadiusX="100" RadiusY="100"/>
                </CombinedGeometry.Geometry2>
            </CombinedGeometry>
        </Path.Data>
    </Path>
</Grid>

MouseMove处理程序:

private void Grid_MouseMove(object sender, MouseEventArgs e)
{
    circle.Center = e.GetPosition((IInputElement)sender);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM