Why won't this hit test fire a second time? wpf
Posted
by csciguy
on Stack Overflow
See other posts from Stack Overflow
or by csciguy
Published on 2010-04-26T17:02:09Z
Indexed on
2010/04/26
17:03 UTC
Read the original article
Hit count: 373
All,
I have a main window that contains two custom objects (AnimatedCharacter). These objects are nothing but images. These images might contain transparent portions. One of these objects slightly overlaps the other object. There is a listener attached to the main window and is as follows.
private void Window_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
Point pt = e.GetPosition((UIElement)sender);
//store off the mouse pt
hitPointMouse = pt;
//clear the result list
hitResultsSubList.Clear();
EllipseGeometry m_egHitArea = new EllipseGeometry(pt, 1, 1);
VisualTreeHelper.HitTest(sender as Visual, HitTestFilterFuncNew, new HitTestResultCallback(HitTestCallback), new GeometryHitTestParameters(m_egHitArea));
//Check all sub items you have now hit
if (hitResultsSubList.Count > 0)
{
CheckSubHitItems(hitResultsSubList);
}
}
The idea is to filter out only a select group of items (called AnimatedCharacters). The hittest and filters are as follows
public HitTestResultBehavior HitTestCallback(HitTestResult htrResult)
{
IntersectionDetail idDetail = ((GeometryHitTestResult)htrResult).IntersectionDetail;
switch (idDetail)
{
case IntersectionDetail.FullyContains:
return HitTestResultBehavior.Continue;
case IntersectionDetail.Intersects:
return HitTestResultBehavior.Continue;
case IntersectionDetail.FullyInside:
return HitTestResultBehavior.Continue;
default:
return HitTestResultBehavior.Stop;
}
}
public HitTestFilterBehavior HitTestFilterFuncNew(DependencyObject potentialHitTestTarget)
{
if (potentialHitTestTarget.GetType() == typeof(AnimatedCharacter))
{
hitResultsSubList.Add(potentialHitTestTarget as AnimatedCharacter);
}
return HitTestFilterBehavior.Continue;
}
This returns me back a list (called hitResultsSubList) that I attempt to then process further. I want to take everything in the hitResultsSubList and run a hit test on it again. This time, the hit test will be checking alpha levels on the particular animatedCharacter object.
private void CheckSubHitItems(List<DependencyObject> hitResultsSub)
{
for(int i = 0; i<hitResultsSub.Count; i++)
{
hitResultsList.Clear();
AnimatedCharacter ac = hitResultsSub[i] as AnimatedCharacter;
try
{
//DEBUGGER SKIPS THIS NEXT LINE EVERY SINGLE TIME.
VisualTreeHelper.HitTest(ac, null, new HitTestResultCallback(hitCallBack), new PointHitTestParameters(hitPointMouse));
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
if (hitResultsList.Count > 0)
{
//do something here
}
}
}
Here is my problem now. The hit test in the second function (CheckSubHitItems) never gets called. There are definitely items (DependencyObjects of the type AnimatedCharacter) in the hitResultSub, but no matter what, the second hit test will not fire. I can walk the for loop fine, but when that line is hit, I get the following console statement.
Step into: Stepping over non-user code 'System.MulticastDelegate.CtorClosed'
No exceptions are thrown.
Any help is appreciated.
© Stack Overflow or respective owner