gpt4 book ai didi

c# - 如何在工具提示在 C# 上时检查鼠标单击

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:24 26 4
gpt4 key购买 nike

在我的代码生成的下图中,

我希望工具提示在光标位于其上时显示每种颜色的值,当我单击图像上的特定位置时,我希望图像上出现虚线。

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();
}

在 wafermap 类中我们有这个:

 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;
}

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

最佳答案

您的问题可能是 getParamValueFromMousePointerXY 执行时间太长,以至于您的 UI 线程无法执行任何其他任务,例如处理您的点击。

您可以将工作卸载到后台任务并将工具提示设置编码回 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

这里唯一需要注意的是,您可能会在此处运行大量后台任务,这些任务将处于竞争状态以设置工具提示。您可以使用取消 token 来防止这种情况发生。您为 CancellationTokenSource 定义一个变量:

CancellationTokenSource tooltipSource = null;

您可以使用此取消 token 源来防止对工具提示进行旧更新:

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();

有了这个,您应该减少工具提示的更新次数。

当然,您可以将 CancellationToken 传递给 getParamValueFromMousePointerXY 并更早地取消任务。

关于c# - 如何在工具提示在 C# 上时检查鼠标单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40828095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com