【问题标题】:WPF. Is it possible to do ellipse "rectangle bounds" hittest?WPF。是否可以做椭圆“矩形边界”命中测试?
【发布时间】:2016-09-26 13:01:22
【问题描述】:

是否可以在椭圆绑定矩形中进行命中测试,like on this image

【问题讨论】:

    标签: c# wpf hittest


    【解决方案1】:

    您可以将它们都放入 border 网格中并检查它是否被点击

    XAML:

                <Grid MouseDown="Border_MouseDown">
                    <Rectangle Width="100"
                               Height="100"
                               Fill="Green" />
                    <Ellipse Width="100"
                             Height="100"
                             Fill="Orange" />
                </Grid>
    

    后面的代码

       private void Border_MouseDown(object sender, MouseButtonEventArgs e)
            {
                MessageBox.Show("hit it");
            }
    

    编辑 为了使它完整,这里只提供了适用于绿色区域的 XAML:

       <Grid>
                <Rectangle Width="100"
                           Height="100"
                           Fill="Green"
                           MouseDown="Border_MouseDown" />
                <Ellipse Width="100"
                         Height="100"
                         Fill="Orange" />
            </Grid>
    

    【讨论】:

    • 在橙​​色区域内点击不会触发吗?他希望它只在绿色区域内开火。
    • 从问题本身对我来说并不清楚。它说矩形边界...
    • @ScottChamberlain 我添加了 XAML 代码来回答您对问题的解释。
    • @ScottChamberlain “他希望它只在绿色区域开火”。你怎么知道的? IMO“我需要在这个绿色区域中的 hittest”意味着“我需要在这个绿色区域中的 hittest ”。
    • @Mat 为什么是边框?您也可以将 MouseDown 处理程序附加到 Grid。你甚至不需要矩形,因为你可以简单地设置网格的背景。
    猜你喜欢
    • 2010-12-18
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多