none
指针事件的问题 RRS feed

  • 问题

  • <Grid  >
            <Canvas x:Name="ceshi" HorizontalAlignment="Left" Height="620" Margin="10,10,0,0" VerticalAlignment="Top" Width="380" PointerEntered="ceshi_PointerEntered" PointerExited="ceshi_PointerExited">
                <TextBlock x:Name="textblock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="390" Width="300" Canvas.Left="40" Canvas.Top="93"/>
            </Canvas>
        </Grid>

    后台

    public MainPage()
            {
                this.InitializeComponent();
                ceshi.PointerEntered += new PointerEventHandler(ceshi_PointerEntered);
                ceshi.PointerExited += new PointerEventHandler(ceshi_PointerExited);
                this.Loaded += new RoutedEventHandler(Rect_Loaded);
            }

    private void ceshi_PointerEntered(object sender, PointerRoutedEventArgs e)
            {
                start = e.GetCurrentPoint(textblock1).Position;       
            }

            private void ceshi_PointerExited(object sender, PointerRoutedEventArgs e)
            {
                Point end = e.GetCurrentPoint(textblock1).Position;
                double angle = 0;
                if (Math.Abs(end.X - start.X) < 1 && Math.Abs(end.Y - start.Y) < 1)
                {
                    angle = 0;
                }
                else if (end.X > start.X)
                {
                    if (end.Y > start.Y)
                    {
                        angle = 360 - Math.Atan((end.Y - start.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI;
                    }
                    else
                    {
                        angle = Math.Atan((start.Y - end.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI;
                    }
                }
                else if (end.X < start.X)
                {
                    if (end.Y > start.Y)
                    {
                        angle = Math.Atan((end.Y - start.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI + 180;
                    }
                    else
                    {
                        angle = 180 - Math.Atan((start.Y - end.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI;
                    }
                }
                if (angle >= 45 && angle < 135)
                {
                   
                    m = "w";
                }
                else if (angle <= 45 || angle > 315)
                {
                   
                    m = "d";
                }
                else if (angle >= 135 && angle < 225)
                {
                   
                    m = "a";
                }
                else if (angle >= 225 && angle < 315)
                {
                   
                    m = "s";
                }
            }

    为什么只有textblock区域可以监控的指针事件?我明明在Canvas添加的事件处理?

    2014年12月10日 5:47

答案

  • 你好,

    >>为什么只有textblock区域可以监控的指针事件?我明明在Canvas添加的事件处理?

    这个问题和WPF中的Panel类似,比如说可以看这个WPF的Panel文档:http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.background(v=vs.110).aspx

    Remarks:

    Panel elements do not receive mouse or stylus events if a Background is not defined. If you need to handle mouse or stylus events but do not want a background for your Panel, use Transparent.

    意思是如果一个面板控件的背景色没有指定,是不会接受鼠标和手写笔的事件的,我们要是想要响应,可以使用透明色,即 Background="Transparent"

    <Canvas x:Name="ceshi" HorizontalAlignment="Left" Background="Transparent" Height="620" Margin="10,10,0,0" VerticalAlignment="Top" Width="380" PointerEntered="ceshi_PointerEntered" PointerExited="ceshi_PointerExited">
                <TextBlock x:Name="textblock1" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Height="390" Width="300" Canvas.Left="40" Canvas.Top="93" Text="This is a TextBlock" Foreground="#FFF90000"/>
            </Canvas>

    这样在我这边测试,一切正常。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 崔東 2014年12月11日 10:27
    2014年12月11日 9:55
    版主