WPF HitTestResult
using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp81 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { List<Visual> visualsList = new List<Visual>(); public MainWindow() { InitializeComponent(); Title = "Hosting DrawingVisuals"; Width = 300; Height = 350; DrawingVisual bodyVisual = new DrawingVisual(); DrawingVisual eyesVisual = new DrawingVisual(); DrawingVisual mouthVisual = new DrawingVisual(); using (DrawingContext dc = bodyVisual.RenderOpen()) { dc.DrawGeometry(Brushes.Blue, null, Geometry.Parse(@"M 240,250 C 200, 375 200,250 175,200 C 100, 400 100,250 100,200 C 0,350 0,250 30,130 C 75,0 100,0 150,0 C 200,0 250,0 250,150 Z")); } using (DrawingContext dc = eyesVisual.RenderOpen()) { dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 10), new Point(95, 95), 15, 15); dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 10), new Point(170, 105), 15, 15); } using (DrawingContext dc = mouthVisual.RenderOpen()) { Pen p = new Pen(Brushes.Black, 10); p.StartLineCap = PenLineCap.Round; p.EndLineCap = PenLineCap.Round; dc.DrawLine(p, new Point(75, 160), new Point(175, 150)); } visualsList.Add(bodyVisual); visualsList.Add(eyesVisual); visualsList.Add(mouthVisual); foreach (Visual vl in visualsList) { AddVisualChild(vl); AddLogicalChild(vl); } } protected override int VisualChildrenCount { get { return visualsList.Count; } } protected override Visual GetVisualChild(int index) { if (index < 0 || index >= visualsList.Count) { throw new ArgumentOutOfRangeException("index"); } return visualsList[index]; } protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); Point location=e.GetPosition(this); HitTestResult result=VisualTreeHelper.HitTest(this, location); if(result.VisualHit.GetType()==typeof(DrawingVisual)) { DrawingVisual dv=result.VisualHit as DrawingVisual; if(dv.Transform==null) { dv.Transform = new RotateTransform(); } (dv.Transform as RotateTransform).Angle++; } } private HitTestResultBehavior HitTestCallback(HitTestResult result) { if(result.VisualHit.GetType()== typeof(HitTestResult)) { DrawingVisual dv=result.VisualHit as DrawingVisual; if(dv.Transform==null) { dv.Transform = new RotateTransform(); } (dv.Transform as RotateTransform).Angle++; } return HitTestResultBehavior.Continue; } protected override void OnMouseRightButtonDown(MouseButtonEventArgs e) { base.OnMouseRightButtonDown(e); Point location = e.GetPosition(this); HitTestResult result = VisualTreeHelper.HitTest(this, location); if (result.VisualHit.GetType() == typeof(DrawingVisual)) { DrawingVisual dv = result.VisualHit as DrawingVisual; if (dv.Transform == null) { dv.Transform = new RotateTransform(); } (dv.Transform as RotateTransform).Angle--; } } } }