Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.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_Canvas_Checkbox_Mouseevent - Fatal编程技术网

C# 启用复选框时,在画布上存储两个鼠标单击位置

C# 启用复选框时,在画布上存储两个鼠标单击位置,c#,wpf,canvas,checkbox,mouseevent,C#,Wpf,Canvas,Checkbox,Mouseevent,我有一个复选框,当我点击它时,它应该允许用户点击画布上的两个地方,然后出现一个带有两次点击坐标的messagebox,然后复选框应该取消选中。我试过各种方法,但总是遇到一些问题 选中该框是RoutedEventArgs,其中在画布上单击是鼠标按钮EventArgs。 我无法存储第二次鼠标单击,第一次单击出现两次;我尝试过各种for循环、while循环等。 无论我将.Checked==false放置在何处,我都无法在消息框出现后取消复选框本身。我得到一个错误,说system.windows.etc

我有一个复选框,当我点击它时,它应该允许用户点击画布上的两个地方,然后出现一个带有两次点击坐标的messagebox,然后复选框应该取消选中。我试过各种方法,但总是遇到一些问题

选中该框是RoutedEventArgs,其中在画布上单击是鼠标按钮EventArgs。 我无法存储第二次鼠标单击,第一次单击出现两次;我尝试过各种for循环、while循环等。 无论我将.Checked==false放置在何处,我都无法在消息框出现后取消复选框本身。我得到一个错误,说system.windows.etcetc.checked只能在+=或-=的左侧显示。 我希望在一个与复选框(也称为routedeventargs)相关的函数中处理整个问题,而不是使用canvas click方法

我能算出2,但1和3让我难倒了

这是从xaml中的画布鼠标向下订阅的方法示例:

   public void get_Scaling(object sender, MouseButtonEventArgs e)
    {
        Point startPoint;
        Point endPoint;

        while (Scale_btn.IsChecked == true)
        {
            startPoint = e.GetPosition(canvas1);

            endPoint = e.GetPosition(canvas1);

            System.Windows.MessageBox.Show("Start point is" + startPoint + "and end point is" + endPoint, "test", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }

我看到了几个问题

1以编程方式取消选中复选框的方法是使用IsChecked属性

Scale_btn.IsChecked = false;
2请记住,while循环在单个MouseDown事件处理程序中运行。您将无法在while循环中捕获两个不同的MouseDown事件。为了实现目标,需要将点对象置于事件处理程序之外,并使用另一个变量来跟踪要捕获的单击

    bool firstPointCaptured = false;
    Point startPoint;
    Point endPoint;

    private void get_Scaling(object sender, MouseButtonEventArgs e)
    {

        if (Scale_btn.IsChecked == true)
        {
            if (!firstPointCaptured)
            {
                startPoint = Mouse.GetPosition(canvas1);
                firstPointCaptured = true;
            }
            else
            {
                endPoint = Mouse.GetPosition(canvas1);
                System.Windows.MessageBox.Show("Start point is" + startPoint + "and end point is" + endPoint, "test", MessageBoxButton.OK, MessageBoxImage.Information);
                Scale_btn.IsChecked = false;
                firstPointCaptured = false;
            }
        }
    }

我看到了几个问题

1以编程方式取消选中复选框的方法是使用IsChecked属性

Scale_btn.IsChecked = false;
2请记住,while循环在单个MouseDown事件处理程序中运行。您将无法在while循环中捕获两个不同的MouseDown事件。为了实现目标,需要将点对象置于事件处理程序之外,并使用另一个变量来跟踪要捕获的单击

    bool firstPointCaptured = false;
    Point startPoint;
    Point endPoint;

    private void get_Scaling(object sender, MouseButtonEventArgs e)
    {

        if (Scale_btn.IsChecked == true)
        {
            if (!firstPointCaptured)
            {
                startPoint = Mouse.GetPosition(canvas1);
                firstPointCaptured = true;
            }
            else
            {
                endPoint = Mouse.GetPosition(canvas1);
                System.Windows.MessageBox.Show("Start point is" + startPoint + "and end point is" + endPoint, "test", MessageBoxButton.OK, MessageBoxImage.Information);
                Scale_btn.IsChecked = false;
                firstPointCaptured = false;
            }
        }
    }
是如何获得鼠标坐标的,您不需要e,因此您的问题1无效

当前您的起始点==端点。您是否理解在显示MessageBox之前,您必须在记住点的同时获得2次鼠标点击事件

您必须使用属性而不是事件来更改复选框的选中状态

是如何获得鼠标坐标的,您不需要e,因此您的问题1无效

当前您的起始点==端点。您是否理解在显示MessageBox之前,您必须在记住点的同时获得2次鼠标点击事件

您必须使用属性而不是事件来更改复选框的选中状态


就在我检查这个之前,我尝试了一些带有布尔标志的东西,这证实了这一点!感谢您澄清“已检查”与“已检查”。我一直认为“Checked”是用于选中/取消选中复选框的控件。就在我选中此复选框之前,我正在尝试带有bool标志的东西,这证实了这一点!感谢您澄清“已检查”与“已检查”。我一直认为“选中”是用于选中/取消选中框的控件。谢谢你的第一点,这使我正在处理的其他一些事情更容易。谢谢你的第一点,这使我正在处理的其他一些事情更容易。