Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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# 找到鼠标越过的点速度太慢_C#_Wpf - Fatal编程技术网

C# 找到鼠标越过的点速度太慢

C# 找到鼠标越过的点速度太慢,c#,wpf,C#,Wpf,好的,现在当我点击网格时,它会穿过网格上的所有“路径”,检查它们是否是椭圆,鼠标是否在它们上面,如果选择了2个,它会计算出间隔的距离。。。。但是如果我在网格上得到50+个点,点击一个点什么也做不了。。。。有没有更有效的方法 这是我的密码: foreach (var x in GraphPanel.Children) { try { if (((Path)x).IsMouseOver && ((Path)x).Data.ToString() =

好的,现在当我点击网格时,它会穿过网格上的所有“路径”,检查它们是否是椭圆,鼠标是否在它们上面,如果选择了2个,它会计算出间隔的距离。。。。但是如果我在网格上得到50+个点,点击一个点什么也做不了。。。。有没有更有效的方法

这是我的密码:

foreach (var x in GraphPanel.Children)
{
     try
     {
         if (((Path)x).IsMouseOver && ((Path)x).Data.ToString() == "System.Windows.Media.EllipseGeometry")
         {
            listViewPoints.SelectedItems.Add(listViewPoints.Items[PointIndex]);
            var converter = new System.Windows.Media.BrushConverter();
            var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FFB1D100");
                    ((Path)x).Stroke = brush;
                    ((Path)x).StrokeThickness = 8;

            if (listViewPoints.SelectedItems.Count == 2)
            {
                 double diffX;
                 double diffY;

                 double ptAX = ((Points)listViewPoints.SelectedItems[0]).A;
                 double ptAY = ((Points)listViewPoints.SelectedItems[0]).B;
                 double ptBX = ((Points)listViewPoints.SelectedItems[1]).A;
                 double ptBY = ((Points)listViewPoints.SelectedItems[1]).B;

                 if (ptAX > ptBX)
                 {
                     diffX = ptAX - ptBX;
                 }
                 else
                 {
                     diffX = ptBX - ptAX;
                 }
                 if (ptAY > ptBY)
                 {
                     diffY = ptAY - ptBY;
                 }
                 else
                 {
                     diffY = ptBY - ptAY;
                 }
                 //the distance between the points = sqr/-diff in x squared + diff in y squared
                 double result = Math.Sqrt(Math.Pow(diffX,2) + Math.Pow(diffY,2));
                 CalculatedDistanceApart.Content = "Distance Apart: " + result.ToString() + "'";
             }
        }

        if (((Path)x).Data.ToString() == "System.Windows.Media.EllipseGeometry")
        {
            PointIndex++;
        }
    }
    catch { }
}

您可以将椭圆添加到网格中,只需将事件处理程序附加到每个椭圆,而不用使用路径


您不需要所有这些命中测试代码。

您可以将椭圆添加到网格中,并在每个椭圆上附加一个事件处理程序,而不用使用路径


您不需要所有这些命中测试代码。

我不知道您的缓慢的
是怎么回事,但整个过程肯定可以加快和整理:

//why make these for every item?
var converter = new System.Windows.Media.BrushConverter(); 
var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FFB1D100"); 

foreach (var x in GraphPanel.Children) 
{ 
    //this is tidier, but what if it's not a Path, is that possible?
    var path = (Path)x;

    //jump out here if it's not the right type, reduces nesting and removes one comparison
    if(!(path.Data is System.Windows.Media.EllipseGeometry)) continue;

     try
     {
         if (path.IsMouseOver)
         { 
            listViewPoints.SelectedItems.Add(listViewPoints.Items[PointIndex]); 
            path.Stroke = brush; 
            path.StrokeThickness = 8;      
            if (listViewPoints.SelectedItems.Count == 2) 
            {
                 double ptAX = ((Points)listViewPoints.SelectedItems[0]).A; 
                 double ptAY = ((Points)listViewPoints.SelectedItems[0]).B; 
                 double ptBX = ((Points)listViewPoints.SelectedItems[1]).A; 
                 double ptBY = ((Points)listViewPoints.SelectedItems[1]).B; 

                 //you're going to square these, so negatives don't matter
                 double diffX = ptAX - ptBX;
                 double diffY = ptAY - ptBY;

                 //the distance between the points = sqr/-diff in x squared + diff in y squared 
                 //don't use Math.Pow for squaring
                 double result = Math.Sqrt(diffX*diffX + diffY*diffY); 
                 CalculatedDistanceApart.Content = "Distance Apart: " + result + "'"; 
                 //are we done now?
             } 
        } 
        PointIndex++; 

    } 
    catch { } 
} 

我不知道你的慢动作
是怎么回事,但整个过程肯定可以加快和整理:

//why make these for every item?
var converter = new System.Windows.Media.BrushConverter(); 
var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FFB1D100"); 

foreach (var x in GraphPanel.Children) 
{ 
    //this is tidier, but what if it's not a Path, is that possible?
    var path = (Path)x;

    //jump out here if it's not the right type, reduces nesting and removes one comparison
    if(!(path.Data is System.Windows.Media.EllipseGeometry)) continue;

     try
     {
         if (path.IsMouseOver)
         { 
            listViewPoints.SelectedItems.Add(listViewPoints.Items[PointIndex]); 
            path.Stroke = brush; 
            path.StrokeThickness = 8;      
            if (listViewPoints.SelectedItems.Count == 2) 
            {
                 double ptAX = ((Points)listViewPoints.SelectedItems[0]).A; 
                 double ptAY = ((Points)listViewPoints.SelectedItems[0]).B; 
                 double ptBX = ((Points)listViewPoints.SelectedItems[1]).A; 
                 double ptBY = ((Points)listViewPoints.SelectedItems[1]).B; 

                 //you're going to square these, so negatives don't matter
                 double diffX = ptAX - ptBX;
                 double diffY = ptAY - ptBY;

                 //the distance between the points = sqr/-diff in x squared + diff in y squared 
                 //don't use Math.Pow for squaring
                 double result = Math.Sqrt(diffX*diffX + diffY*diffY); 
                 CalculatedDistanceApart.Content = "Distance Apart: " + result + "'"; 
                 //are we done now?
             } 
        } 
        PointIndex++; 

    } 
    catch { } 
} 

我不是手动绘制椭圆,它们是通过编程添加的,而且我已经在代码中使用了路径的功能来在正确的位置绘制点,是否有更好的方法不更改椭圆路径?恐怕没有。与几何图形相反的形状背后的想法是,它们恰好添加了几何图形;这是最成功的。因此,如果你不使用这些形状,你将不得不自己处理,我猜这会更慢。我将尝试编写一个线程解决方案,看看这是否有什么不同,如果没有,我想我可以将我的代码转换为不使用路径,我不记得为什么我从使用路径开始(因为我很久以前写过代码)但我想我有一个很好的理由:构建一个线程化的解决方案会很困难,因为你访问的UI只能在UI线程上完成。只有当你有可用的数据而不需要返回UI元素时,它才会起作用。我不是手动绘制省略号,它们是通过编程方式添加的,我已经使用了一些功能在我的代码中,有一条路径可以在正确的位置绘制点,在不将路径更改为椭圆的情况下,有没有更好的方法呢?恐怕没有。形状与几何相反,其背后的想法是它们添加了精确的命中测试。因此,如果你不使用形状,你将不得不自己进行处理,我猜是这样的我将尝试编写一个线程解决方案,看看这是否有什么不同,如果没有,我想我可以将我的代码转换为不使用路径,我不记得为什么要使用路径开始(因为我很久以前写过代码)但我想我有一个很好的理由:构建线程化解决方案会很困难,因为您访问的UI只能在UI线程上完成。只有当您有可用的数据而不需要返回UI元素时,它才会起作用。如果不是几何体,继续,思考得好!为什么要为每个项目都创建这些?这只会让您感到惊讶当它找到它结束的路径时,它将停止(它只能超过1条路径)所以它只运行一次:oOK这实际上产生了巨大的差异,每次我点击一个点,它都会立即被选中。你的旧代码执行了
IsMouseOver
,即使它不是
EllipseGeometry
,因为它是
&
的第一部分。这可能是我版本中最大的差异。如果它只能结束一个,那么为什么不在第一次“命中”后将其从循环中打断,以获得额外的优化。如果不是几何图形,请继续,好主意!为什么要为每个项目创建这些?只有在找到结束的路径时才会创建(只能超过1条路径)所以它只运行一次:oOK这实际上产生了巨大的差异,每次我点击一个点,它都会立即被选中。你的旧代码执行了
IsMouseOver
,即使它不是
EllipseGeometry
,因为它是
&
的第一部分。这可能是我版本中最大的差异。如果它只能结束一个,那么为什么不在第一次“命中”后,也将
中断
以获得额外的优化呢。