■ VisualTreeHelper 클래스의 HitTest 정적 메소드에서 히트 테스트 필터 콜백을 처리하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Border BorderThickness="1" BorderBrush="Black"> <Canvas Name="canvas" Width="400" Height="300"> <Rectangle Canvas.Left="50" Canvas.Top="50" Width="100" Height="100" Fill="Red" /> <Rectangle Canvas.Left="75" Canvas.Top="75" Width="100" Height="100" Fill="Green" /> <Rectangle Canvas.Left="100" Canvas.Top="100" Width="100" Height="100" Fill="Blue" /> </Canvas> </Border> <TextBlock Name="textBlock" HorizontalAlignment="Center" Margin="0 10 0 0" Foreground="Blue" /> </StackPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
using System.Collections.Generic; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 히트 테스트 결과 리스트 /// </summary> private List<DependencyObject> histTestResultList = new List<DependencyObject>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); MouseDown += Window_MouseDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 마우스 이동시 처리하기 - Window_MouseDown(sender, e) /// <summary> /// 윈도우 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_MouseDown(object sender, MouseButtonEventArgs e) { Point mousePoint = e.GetPosition(this.canvas); HitTestResult result = VisualTreeHelper.HitTest(this.canvas, mousePoint); this.histTestResultList.Clear(); VisualTreeHelper.HitTest ( this.canvas, new HitTestFilterCallback(ProcessHitTestFilterCallback), new HitTestResultCallback(ProcessHitTestResultCallback), new PointHitTestParameters(mousePoint) ); this.textBlock.Text = $"비주얼 히트 수 : {this.histTestResultList.Count}"; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 히트 테스트 필터 콜백 처리하기 - ProcessHitTestFilterCallback(d) /// <summary> /// 히트 테스트 필터 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <returns>히트 테스트 필터 동작</returns> public HitTestFilterBehavior ProcessHitTestFilterCallback(DependencyObject d) { if(d is Rectangle rectangle) { if(rectangle.Fill == Brushes.Green) { return HitTestFilterBehavior.ContinueSkipSelfAndChildren; } else { return HitTestFilterBehavior.Continue; } } else { return HitTestFilterBehavior.Continue; } } #endregion #region 히트 테스트 결과 콜백 처리하기 - ProcessHitTestResultCallback(result) /// <summary> /// 히트 테스트 결과 콜백 처리하기 /// </summary> /// <param name="result">히트 테스트 결과</param> /// <returns>히트 테스트 결과 동작</returns> public HitTestResultBehavior ProcessHitTestResultCallback(HitTestResult result) { this.histTestResultList.Add(result.VisualHit); return HitTestResultBehavior.Continue; } #endregion } } |