WPF矩形或折线HitTest不起作用

时间:2013-10-31 16:42:22

标签: wpf hittest

我只想在矩形中进行点击测试。

1)定义一个矩形并将其添加到MainWindow_Load中的画布。

        rc = new Rectangle();
        rc.Width = 100;
        rc.Height = 100;
        rc.Fill = Brushes.Yellow;
        canvas.Children.Add(rc);
        Canvas.SetLeft(rc, 20);
        Canvas.SetTop(rc, 30);

2)在mousedown上的Rectangle中测试鼠标点。

 void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
    {
        IInputElement input, input2;

        Point pt = e.GetPosition((UIElement)sender);

       if (rc.InputHitTest(pt) != null)
       {
           MessageBox.Show("Clicked");
       }            
    }

3)此代码不起作用,与折线相同。任何建议都应该表示赞赏。

0 个答案:

没有答案