使用鼠标在WPF C#中移动图像

3
你好,我已经花了一些时间尝试解决这个问题,希望你能引导我找到正确的方向!
目标是在我的wpf游戏中实现“手电筒”效果,为此我追求了两个主要选项。
1.使用一个黑色的png图像,该图像的宽度和高度为窗口的200%,其中心有一个洞,然后通过鼠标移动图像。 2.使用与窗口大小相同的黑色图像,并具有一个不透明度掩码元素,该元素是圆形形状,可以查看黑色层下面的内容。这个圆随着鼠标移动而移动。
我无法使这两个选项都起作用,在wpf中当您拥有不透明度掩码时,似乎无法移动“穿过”图层的元素。对于大型图像方法,我无法将鼠标Point对象转换为图像的位置。以下是获取鼠标位置的潜在方法,我无法将图像移动到“ 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;
}

获取位置信息
var point = e.GetPosition(this.YourControl);

最终的游戏将使用kinect进行控制,因此如果有使用kinect库或原生方法实现的方式,也可以考虑。谢谢。
1个回答

4

您可以使用一个路径元素,其中包含一个非常大的RectangleGeometry和一个被排除(因此是透明的)的EllipseGeometryCombinedGeometry,通过更改其鼠标输入时的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);
}

哇,我欠你一个大人情。我们有三个人在做这个,但都没有成功。真的很简单易懂,移动/更新有点卡顿,但我会看看能否改进它的那一部分。非常感谢!编辑::算了,卡顿是因为透明层下面没有东西。如果有物体在其下面,它就会流畅! - lukeHelper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接