2

我对此有点困惑,但我有一个问题是试图让我的头绕着鼠标捕捉到网格。目前我正在通过OnRender这样的覆盖来绘制网格;

 protected override void OnRender(DrawingContext drawingContext)
        {

            int numberOfVerticalLines = 8;
            int numberOfHorizontalLines = 8;

            CellHeight = this.ActualHeight / numberOfVerticalLines;
            CellWidth = this.ActualWidth / numberOfHorizontalLines;

            double verticalOffset = 0;
            double horizontalOffset = 0;

            Pen pen = new Pen(Stroke, StrokeThickness);

            pen.DashStyle = DashStyle;

            for (int i = 0; i <= numberOfHorizontalLines; i++)
            {
                for (int j = 0; j <= numberOfVerticalLines; j++)
                {

                    drawingContext.DrawLine(pen, new Point(horizontalOffset, verticalOffset), new Point(horizontalOffset, CellHeight + verticalOffset));

                    verticalOffset += CellHeight;
                }

                horizontalOffset += CellWidth;
                verticalOffset = 0;
            }

            horizontalOffset = 0;
            verticalOffset = 0;

            for (int i = 0; i <= numberOfVerticalLines; i++)
            {
                for (int j = 0; j <= numberOfHorizontalLines; j++)
                {
                    drawingContext.DrawLine(pen, new Point(horizontalOffset, verticalOffset), new Point(CellWidth + horizontalOffset, verticalOffset));

                    horizontalOffset += CellWidth;
                }

                verticalOffset += CellHeight;
                horizontalOffset = 0;
            }
        }

这给出了以下结果;

网格示例

但是,我有点坚持考虑将鼠标捕捉到最近的网格交叉点(水平线与垂直线相交)的路线。显然,当我使用绘图上下文来绘制线条时,我在绘制这些线条后没有参考它们。

所以我想基本上我的问题是,我怎样才能实现鼠标对齐网格?这更像是一个数学问题而不是面向对象的控制问题吗?我已经阅读了几乎所有我认为相关的堆栈溢出问题,但还没有得出任何现实的想法。

注意:虽然目前我已经硬编码了一个 8x8 网格,但这最终将由用户定义。

4

3 回答 3

2

一种基本方法是将鼠标的 (x,y) 与十字进行比较: 1. 计算鼠标所在单元格的宽度和高度的起点和终点;2. 将这两个区间(宽度和高度)与实际鼠标 (x,y) 进行比较,以找到最近的单元格点。

下面是一些快速拼凑的示例代码来演示捕捉:

/// <summary>
/// When left shift key is pressed we snap the mouse to the closest
/// intersection
/// </summary>
void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.LeftShift)
    {
        var p = GetSnappingPoint(Mouse.GetPosition(this), new Size(200, 200));
        SetCursorPos((int)p.X, (int)p.Y+20);
    }

}

[DllImport("User32.dll")]
private static extern bool SetCursorPos(int x, int y);

/// <summary>
/// Get snapping point by
/// </summary>
Point GetSnappingPoint(Point mouse,Size cellSize)
{
    //Get x interval based on cell width
    var xInterval = GetInterval(mouse.X, cellSize.Width);

    //Get y interal based in cell height
    var yInterval = GetInterval(mouse.Y, cellSize.Height);

    // return the point on cell grid closest to the mouseposition
    return new Point()
    {
        X = Math.Abs(xInterval.Lower - mouse.X) > Math.Abs(xInterval.Upper - mouse.X) ? xInterval.Upper : xInterval.Lower,
        Y = Math.Abs(yInterval.Lower - mouse.Y) > Math.Abs(yInterval.Upper - mouse.Y) ? yInterval.Upper : yInterval.Lower,
    };
}

/// <summary>
///  Find an interval of the celsize based on mouse position and size
/// </summary>
Interval GetInterval(double mousePos,double size)
{
    return new Interval()
    {
        Lower = ((int)(mousePos / size)) * size,
        Upper = ((int)(mousePos / size)) * size + size
    };
}

/// <summary>
/// Basic interval class
/// </summary>
class Interval
{
    public double Lower { get; set; }
    public double Upper { get; set; }
}
于 2013-07-10T15:38:17.627 回答
1

一个粗略的开始:

int nearGridX = CellWidth * Math.Round( mouseX / CellWidth);
int nearGridY = CellHeight * Math.Round( mouseY / CellHeight);
于 2013-07-10T15:36:40.017 回答
0
xPos= mouseX - (mouseX % gridWidth);
yPos= mouseY - (mouseY % gridHeight);

这将使您快速而肮脏地捕捉到当前网格的左上角。这没有考虑到您是否更靠近当前网格的底部或右侧。它只查看您所在的网格并将其放置在那里。

于 2016-04-21T18:10:09.837 回答