日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 命中测试HitTest

發(fā)布時間:2023/12/4 asp.net 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 命中测试HitTest 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

獲取不規(guī)則圖片的點擊事件,

如果一個Canvas中,有很多圖形,比如下圖:矩形,菱形等。

如果每個圖形都加一個點擊事件,想要一個通用的方法,獲取鼠標(biāo)點擊在了哪個圖形上,這里可以使用VisualTreeHelper.HitTest方法。

直接看看示例代碼:

MainWindow.xaml:

<Window x:Class="wpfcore.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:wpfcore" xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"mc:Ignorable="d"Background="LightBlue"UseLayoutRounding="True"Title="MainWindow" Width="600" Height="340"><WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center" Name="panel" MouseUp="mouseUp"><Path x:Name="矩形1" Margin="5" Fill="Red" Data="M0 0,100 0,100,100,0 100Z"/><Path x:Name="矩形2" Margin="-5" Fill="Green" Data="M0 0,100 0,100,100,0 100Z"><Path.RenderTransform><RotateTransform CenterX="50" CenterY="50" Angle="60"/></Path.RenderTransform></Path><Path x:Name="菱形1" Stroke="Red" Fill="Red"><Path.Data><PathGeometry><PathFigure IsClosed="True" StartPoint="50,0"><LineSegment Point="75,50"></LineSegment><LineSegment Point="50,100"></LineSegment><LineSegment Point="25,50"></LineSegment></PathFigure></PathGeometry></Path.Data></Path><Path x:Name="菱形2" Stroke="Red" Fill="Red"><Path.Data><PathGeometry><PathFigure IsClosed="True" StartPoint="50,0"><LineSegment Point="75,50"></LineSegment><LineSegment Point="50,100"></LineSegment><LineSegment Point="25,50"></LineSegment></PathFigure><PathGeometry.Transform><RotateTransform Angle="-115" CenterX="50" CenterY="100"></RotateTransform></PathGeometry.Transform></PathGeometry></Path.Data></Path></WrapPanel> </Window>

MainWindow.cs:

using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Input; using System.Windows.Media;namespace wpfcore {public partial class MainWindow : Window{public MainWindow(){InitializeComponent();DataContext = this;}private void mouseUp(object sender, MouseButtonEventArgs e){var p = e.GetPosition(sender as UIElement);VisualTreeHelper.HitTest(panel, null, callback, new PointHitTestParameters(p));System.Diagnostics.Debug.WriteLine($"------------------------");}private HitTestResultBehavior callback(HitTestResult result){System.Diagnostics.Debug.WriteLine($"點擊了{result.VisualHit.GetValue(NameProperty)}");return HitTestResultBehavior.Continue;}} }

輸出點擊點在哪個圖形內(nèi),當(dāng)然,你也可以做別的事。

如果喜歡,點個贊唄~

總結(jié)

以上是生活随笔為你收集整理的WPF 命中测试HitTest的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。