我想在用户右键单击某个对象时打开一个上下文菜单,但我想通过它传递所有其他鼠标命中。
protected override HitTestResult HitTestCore( PointHitTestParameters hitTestParameters )
{
var hitPoint = hitTestParameters.HitPoint;
if ( ( _xOffset <= hitPoint.X && hitPoint.X <= _xOffset + _width ) &&
_isRightClick )
{
return new PointHitTestResult( this, hitPoint );
}
return null;
}
如何找出_isRightClick?
欢迎任何更好的架构解决方案。 :)
答案 0 :(得分:1)
为什么不改写onmouseclick / onmouseup方法呢?他们有一个包含mousekey信息的mouseeventargs。
public override void OnMouseUp(EditableGraphicsLayer layer, MouseButtonEventArgs e)
{
if (IsRightButtonChanged(e))
{
// do stuff
}
}
private bool IsRightButtonChanged(MouseButtonEventArgs args)
{
return args.ChangedButton == MouseButton.Right;
}
编辑:或者,根据你的评论,如果你不能覆盖这些方法并且必须单独在hittesting中解决这个问题,也许你可以从静态鼠标类中读取鼠标按钮的状态,例如:
Console.WriteLine(Mouse.RightButton);
答案 1 :(得分:0)
注意:mtjin确实帮助我朝着正确的方向发展。
当inputmanager触发prenotify事件时,我通过使视觉无效来实现他的想法。
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;
}