gpt4 book ai didi

c# - 在 WPF 中按下按钮后检测鼠标单击的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:56 25 4
gpt4 key购买 nike

我的目标是在程序上创建一个直线工具,以便在按下按钮时,用户能够使用鼠标左键选择两个点,第二次单击后,应在所选点之间绘制一条直线点数。

或者类似于 Microsoft 画图上的线条工具(如果更容易的话)。只要用户可以画一条线。

到目前为止,我的代码很少。我遇到的问题是在下面的函数中检测到鼠标点击。我最初使用 while 循环

if (MouseButton.Left == MouseButtonState.Pressed) 

为了检查点击次数,但是我只是创建了一个无限循环,因为条件从未得到满足。

我唯一的其他想法是在 LineTool_Click 函数中使用一个事件,例如 drawingCanvas.MouseDown 但我也不知道它是如何工作的:/我是 c#/wpf 的新手。

// When the LineTool button is clicked.....
private void LineTool_Click(object sender, RoutedEventArgs e)
{
Point startPoint = new Point(0,0);
Point endPoint = new Point(0, 0);
}

最佳答案

最简单的选择可能是将开始/结束点存储在方法的外部,然后在方法内部检查它们是否已被捕获:

// Store these outside of the method
Point lastPoint = new Point(0,0);
bool captured = false;

// When the LineTool button is clicked.....
private void LineTool_Click(object sender, MouseButtonEventArgs e)
{
if (!this.captured)
{
this.captured = true;
this.lastPoint = e.GetPosition(this.LineTool);
return;
}

// Okay - this is the second click - draw our line:
this.captured = false; // Make next click "start" again
Point endPoint = e.GetPosition(this.LineTool);

// draw from this.lastPoint to endPoint
}

关于c# - 在 WPF 中按下按钮后检测鼠标单击的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28156254/

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