2016-11-27 62 views
2

通过我的代码生成以下图像中,如何检查鼠标点击而刀尖是C#

我想要的工具提示中显示每个颜色值,而我的光标就可以了,当我点击一个特定的放置在图像上我想要一条虚线出现在图像上。

Ref Bar Image 这是我的代码:

RefBar.MouseMove += new MouseEventHandler(RefBar_MouseMove); 
 
      RefBar.MouseClick += new MouseEventHandler(RefBar_Click); 
 

 
private void RefBar_MouseMove(object sender, MouseEventArgs e) 
 
     { 
 
      if (gotMapFirstTime == true) 
 
      { 
 
       Point LocalMousePosition = RefBar.PointToClient(System.Windows.Forms.Cursor.Position); 
 
       MousePointDisplay.SetToolTip(RefBar, WaferMap.getParamValueFromMousePointerXY(LocalMousePosition.X, LocalMousePosition.Y, 1, true).ToString());     
 
      } 
 
     } 
 

 
private void RefBar_Click(object sender, EventArgs e) 
 
     { 
 
      byte[] bytes2; 
 
      Image image; 
 
      MouseEventArgs me = (MouseEventArgs)e; 
 
      Point coordinates = me.Location; 
 
      
 
      WaferMap.RefBarDashLines.Add(coordinates.Y); 
 
      
 
      int[] rfd = WaferMap.RefBarDashLines.ToArray(); 
 
      if (rfd.Length > 2) 
 
      { 
 
       RefBar.Image.Dispose(); 
 
       bytes2 = WaferMap.CreateMapReferenceBar(40, 580, 0, 0, 1); 
 
       WaferMap.RefBarDashLines = new List<int>(); 
 
       WaferMap.UpperTrackBarLimit = 0.0; 
 
       WaferMap.LowerTrackBarLimit = 0.0; 
 
       pictureBox2.Image.Dispose(); 
 
       bytes2 = WaferMap.CreateGridImage(120, 120, 9, 9, 5); 
 
       image = Image.FromFile(WaferMapImage); 
 
       pictureBox2.Image = image; 
 
      } 
 
      else if(rfd.Length == 2) 
 
      { 
 
       RefBar.Image.Dispose(); 
 
       bytes2 = WaferMap.CreateMapReferenceBarByClick(40, 580, 0, 0, 1); 
 
       pictureBox2.Image.Dispose(); 
 
       bytes2 = WaferMap.CreateGridImageFilteredByTrackBar(120, 120, 9, 9, 5); 
 
       image = Image.FromFile(WaferMapImage); 
 
       pictureBox2.Image = image; 
 
      } 
 
      else 
 
      { 
 
       RefBar.Image.Dispose(); 
 
       bytes2 = WaferMap.CreateMapReferenceBarByClick(40, 580, 0, 0, 1); 
 
      } 
 
      
 
      image = Image.FromFile(ReferenceBarImage); 
 
      RefBar.Image = image; 
 
      
 
      MapLowerLimit.Text = coordinates.X.ToString() + " " + coordinates.Y.ToString(); 
 
     }

类晶片图,我们有这样的:

public static double getParamValueFromMousePointerXY(int x, int y, int boxSize, bool isRefBarOrHistogram) 
 
     { 
 
      double returnVal = 0.0; 
 
      Point UL; 
 
      Point BR; 
 
      int cellX; 
 
      int invertY; 
 
      int cellY; 
 
      if (isRefBarOrHistogram) 
 
      { 
 
       invertY = -1*(y - RefBarLength); 
 
       return get_YCell_to_ParamValue(invertY, RefBarLength); 
 
      } 
 
      else 
 
      { 
 
       foreach (die dd in dieList) 
 
       { 
 
        cellX = dd.col; 
 
        cellY = dd.row; 
 
        UL = new Point(boxSize * (cellX + 2), boxSize * (cellY + 4)); 
 
        BR = new Point((boxSize * (cellX + 2)) + boxSize, (boxSize * (cellY + 4)) + boxSize); 
 
        if ((UL.X < x && x <= BR.X) && (UL.Y < y && y <= BR.Y)) 
 
        { 
 
         return dd.ParamValue; 
 
        } 
 

 
       } 
 
      } 
 
      
 
      return returnVal; 
 
     } 
 
public struct die 
 
{ 
 
    public int row; 
 
    public int col; 
 
    public int site; 
 
    public string param; 
 
    public double ParamValue; 
 
}

如果工具提示功能被注释掉,鼠标点击事件的代码可以工作,但是当为鼠标移动功能调用工具提示功能时,代码在多次单击鼠标点击事件时不检测或检测到,我该如何纠正这个?

+0

你在'SetToolTip'方法中做了更长时间的计算吗? Click处理程序中的所有内容均在UI线程中执行。在此期间,不能执行其他UI操作。这可能会导致点击无法识别。虽然没有看到方法,但很难说。解决方案是加速该方法或在后台执行其中的一部分。 – Sefe

+0

我编辑了代码供您参考,我无法粘贴整个代码,因为它是一个非常大的应用程序,我只想知道如何调用RefBar_Click函数,同时工具提示处于RefBar_MouseMove函数 – Adhil

+0

tooltip命令非常快,并且无论光标位于图像上,我都能得到正确的值 – Adhil

回答

1

您的问题可能是getParamValueFromMousePointerXY需要很长时间才能执行,以至于您的UI线程被阻止执行任何其他任务,例如处理您的点击。

您可以卸载工作到后台任务和元帅设置tooltip回UI线程:

Task.Run(() => { 
    string paramValue = WaferMap.getParamValueFromMousePointerXY(LocalMousePosition.X, LocalMousePosition.Y, 1, true).ToString(); 
    MethodInvoker setTooltip = delegate() { 
     MousePointDisplay.SetToolTip(RefBar, paramValue); 
    }; 
    RefBar.Invoke(setTooltip); 
}); 

什么,你基本上这里做的是在后台任务执行getParamValueFromMousePointerXY,而你继续在UI线程中执行SetToolTip

这里唯一需要注意的是,您可能会在这里运行很多后台任务,这些任务将处于竞争状态以设置工具提示。您可以通过使用取消标记来阻止该问题。你定义一个变量为CancellationTokenSource

CancellationTokenSource tooltipSource = null; 

您可以使用该取消标记源,以防止旧的更新的提示:

tooltipSource?.Cancel(); 
tooltipSource = new CancellationTokenSource(); 

Task tooltipTask = new Task((tokenObj) => { 
    string paramValue = WaferMap.getParamValueFromMousePointerXY(LocalMousePosition.X, LocalMousePosition.Y, 1, true).ToString(); 
    ((CancellationToken)tokenObj).ThrowIfCancellationRequested(); 
    MethodInvoker setTooltip = delegate() { 
     MousePointDisplay.SetToolTip(RefBar, paramValue); 
    }; 
    RefBar.Invoke(setTooltip); 
}, tooltipSource.Token); 
tooltipTask.Start(); 

有了这个,你应该更新的数量减少到您的提示。

当然,您可以通过CancellationTokengetParamValueFromMousePointerXY并更早取消任务。

+2

您仍然应该认真考虑优化getParamValueFromMousePointerXY。即使您不阻止UI线程,但如果工具提示没有立即出现,UI也会被破坏。 –