I want to open a context menu when the user right clicks on an object but I want to pass all other mouse hits through it.
protected override HitTestResult HitTestCore( PointHitTestParameters hitTestParameters )
{
var hitPoint = hitTestParame开发者_Go百科ters.HitPoint;
if ( ( _xOffset <= hitPoint.X && hitPoint.X <= _xOffset + _width ) &&
_isRightClick )
{
return new PointHitTestResult( this, hitPoint );
}
return null;
}
How do I figure out _isRightClick?
Any better architected solutions are welcome. :)
why not override the onmouseclick / onmouseup methods instead? they have a mouseeventargs that includes mousekey info.
public override void OnMouseUp(EditableGraphicsLayer layer, MouseButtonEventArgs e)
{
if (IsRightButtonChanged(e))
{
// do stuff
}
}
private bool IsRightButtonChanged(MouseButtonEventArgs args)
{
return args.ChangedButton == MouseButton.Right;
}
EDIT: or, based on your comment, if you cannot override those methods and have to solve this in hittesting alone, maybe you could read the state of the mouse buttons from the static Mouse class like this for example:
Console.WriteLine(Mouse.RightButton);
NOTE: mtjin really helped push me in the right direction.
I got his idea to work by invalidating the visual when the prenotify event gets fired by inputmanager.
public MyClass()
{
InitializeComponent();
InputManager.Current.PreNotifyInput += InputManagerPreNotifyInput;
}
private void InputManagerPreNotifyInput( object sender, NotifyInputEventArgs e )
{
var mouseEventArgs = e.StagingItem.Input as MouseEventArgs;
if ( mouseEventArgs == null )
return;
if ( mouseEventArgs.RoutedEvent == PreviewMouseDownEvent )
{
InvalidateVisual();
}
}
protected override HitTestResult HitTestCore( PointHitTestParameters hitTestParameters )
{
var hitPoint = hitTestParameters.HitPoint;
if ( ( _xOffset <= hitPoint.X && hitPoint.X <= _xOffset + _width ) &&
Mouse.RightButton == MouseButtonState.Pressed )
{
return new PointHitTestResult( this, hitPoint );
}
return null;
}
精彩评论