Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中移动行_C#_Wpf - Fatal编程技术网

C# 在WPF中移动行

C# 在WPF中移动行,c#,wpf,C#,Wpf,我需要实现移动newLine\u MouseMove的功能,这样我就可以用鼠标在画布上移动线条,而不仅仅是在右下角,如code所示: 代码: private void newLine_MouseMove(object sender, MouseEventArgs e) { if (Mouse.LeftButton == MouseButtonState.Pressed) //Movement only when the key is pressed { if (

我需要实现移动
newLine\u MouseMove
的功能,这样我就可以用鼠标在画布上移动线条,而不仅仅是在右下角,如
code
所示:

代码:

private void newLine_MouseMove(object sender, MouseEventArgs e)
{
    if (Mouse.LeftButton == MouseButtonState.Pressed) //Movement only when the key is pressed
    {
        if (ExitLine())  //The function returns a value true, if the line is within the Canvas.
        {
            if (selectedLine != null) //If you select a line
            {
                //????????????????????
                /*
                selectedLine.X1 += 1;
                selectedLine.Y1 += 1;
                selectedLine.X2 += 1;
                selectedLine.Y2 += 1;
                */
            }
        }
    }
}
我很想得到一个详细的答案


谢谢。

此代码非常适合我。您需要更改
部分中的一些代码,但将
MouseMove=“”
定义保留在此处:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="525" Width="525"
        MouseMove="Line_MouseMove">

    <Line Name="L1" X2="50" Y2="50" Stroke="Red" StrokeThickness="1" />

</Window>
这就是这里正在发生的事情。此代码创建以下行:
。当鼠标在窗口上移动时,窗口调用
MouseMove
事件,该事件调用
Line\u MouseMove
方法。
Line\u MouseMove
方法将光标相对于直线的位置添加到
X
Y
坐标中,从而保持直线的长度和角度,但保持直线的起始位置不变。要更改直线的长度和角度,只需更改
X2
Y2
的值

如果需要将线路限制在某个区域,请将
line\u MouseMove
替换为:

private void Line_MouseMove(object sender, MouseEventArgs e)
{
    // create limited rectangle
    Rect rr = new Rect(100, 50, 60, 400);

    // check if both ends of the line are fully contained within the rectangle
    if (rr.Contains(e.GetPosition(L1).X, e.GetPosition(L1).Y) && rr.Contains(e.GetPosition(L1).X + L1.X2 - L1.X1, e.GetPosition(L1).Y + L1.Y2 - L1.Y1))
    {
        // change the line's position
        L1.X2 = L1.X2 - L1.X1 + e.GetPosition(L1).X;
        L1.Y2 = L1.Y2 - L1.Y1 + e.GetPosition(L1).Y;
        L1.X1 = e.GetPosition(L1).X;
        L1.Y1 = e.GetPosition(L1).Y;
    }
}
Rect(0,0,200,200)
中的坐标替换为所需的坐标-您可能需要根据窗口大小计算它们


上述解决方案允许线路末端从指定区域消失

什么是“平滑线”?这个代码应该做什么?您真的希望只要
ExitLine()
方法返回
true
,该行就沿对角线向右下方移动吗?对于“详细的答案”,我强烈推荐一个详细的问题。这两者通常是同时进行的。此代码示例移动右下角的线条,我需要在整个桌面显示中移动鼠标线以外的位置。如果希望移动跟随鼠标,则需要使用鼠标坐标(可在
MouseEventArgs
对象中获得)。通常这意味着在第一次按下鼠标按钮时保存坐标,然后使用当前位置和该起始位置之间的差值来确定对象位置或边界的实际变化。但如果没有更好的问题,就不可能确定什么对你有效。什么是ExitLine()?为什么只在返回
true
时才移动对象?我怀疑处理
Line
MouseMove
事件不会正常工作。尤其是当你试着把线移动得尽可能快的时候。@KingKing我不知道你这是什么意思。在
行中处理
MouseMove
不起作用,但在
窗口中处理它确实起作用。实际上,处理程序名欺骗了我。它的名称是
Line\u MouseMove
,但实际上这是
窗口的
MouseMove
处理程序。因此,它当然可以工作。我尝试在
行中处理它,但随后将它更改为
窗口。这也是我首先做的,但注意到了问题(当尽可能快地移动它时)。
private void Line_MouseMove(object sender, MouseEventArgs e)
{
    // create limited rectangle
    Rect rr = new Rect(100, 50, 60, 400);

    // check if both ends of the line are fully contained within the rectangle
    if (rr.Contains(e.GetPosition(L1).X, e.GetPosition(L1).Y) && rr.Contains(e.GetPosition(L1).X + L1.X2 - L1.X1, e.GetPosition(L1).Y + L1.Y2 - L1.Y1))
    {
        // change the line's position
        L1.X2 = L1.X2 - L1.X1 + e.GetPosition(L1).X;
        L1.Y2 = L1.Y2 - L1.Y1 + e.GetPosition(L1).Y;
        L1.X1 = e.GetPosition(L1).X;
        L1.Y1 = e.GetPosition(L1).Y;
    }
}