Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could add a mouse down event handler to the Canvas, instead of the Shape objects, and then do <a href="http://msdn.microsoft.com/en-us/library/ms752097.aspx" rel="nofollow">Hit Testing in the Visual Layer</a> (although the API is a bit strange) with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.geometryhittestparameters.aspx" rel="nofollow">hit test geometry</a>, for example an ellipse. The Canvas needs to have its <code>Background</code> set (e.g. to <code>Transparent</code>) to receive mouse events.</p> <p>Sorry that this is C#, but i don't speak VB:</p> <pre><code>private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Canvas canvas = sender as Canvas; EllipseGeometry hitTestGeometry = new EllipseGeometry(e.GetPosition(canvas), 10d, 10d); Shape hitShape = null; HitTestResultCallback hitTestCallback = result =&gt; { hitShape = result.VisualHit as Shape; return hitShape != null ? HitTestResultBehavior.Stop : HitTestResultBehavior.Continue; }; VisualTreeHelper.HitTest(canvas, null, hitTestCallback, new GeometryHitTestParameters(hitTestGeometry)); if (hitShape != null) { System.Diagnostics.Trace.TraceInformation("hit shape: {0}", hitShape); } } </code></pre> <p><strong>EDIT:</strong></p> <p>Here is the equivalent VB code. VB does not support multiline lambda expressions so the hit test callback has to be declared explicitly</p> <pre><code>Private Function htCallback(ByVal result As HitTestResult) _ As HitTestResultBehavior Dim hitShape As Shape = Nothing hitShape = TryCast(result.VisualHit, Shape) If hitShape IsNot Nothing Then 'do something End If Return If(hitShape IsNot Nothing, HitTestResultBehavior.[Stop], _ HitTestResultBehavior.[Continue]) End Function Private Sub Canvas_MouseLeftButtonDown(ByVal sender As Object, _ ByVal e As MouseButtonEventArgs) Handles Canvas1.MouseRightButtonDown Dim canvas As Canvas = TryCast(sender, Canvas) Dim hitTestGeometry As New EllipseGeometry(e.GetPosition(canvas), 10.0, 10.0) Dim hitTestCallback As HitTestResultCallback = _ New HitTestResultCallback(AddressOf htCallback) VisualTreeHelper.HitTest(canvas, Nothing, hitTestCallback, _ New GeometryHitTestParameters(hitTestGeometry)) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload