【问题标题】:Move image with mouse wpf c#用鼠标wpf c#移动图像
【发布时间】:2016-02-23 13:34:14
【问题描述】:

您好,我已经尝试解决这个问题一段时间了, 我希望你能带领我走向正确的方向!

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

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

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

指向屏幕

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 库或本机实现这一点,这也是一种选择。谢谢。

【问题讨论】:

    标签: c# wpf visual-studio mouse kinect


    【解决方案1】:

    您可以使用带有 CombinedGeometry 的 Path 元素,该元素由一个非常大的 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);
    }
    

    【讨论】:

    • 哇,我欠你很多时间。我们有 3 个人在做这件事,但我们都没有成功。非常简单易懂,移动/更新有点不稳定,但我会看看我是否可以改进这部分。太感谢了!编辑:: 没关系,波涛汹涌的是当透明水平下方没有任何东西时。它与下面的物体一起平滑!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多