Simuliert ein Klickereignis (Linke Maustaste) für ein Shape-Objekt.
/// <summary>
/// Verwaltet die nötigen Einstellungen zum Auslösen eines Linksklicks auf einer (un-)transformierten Form.
/// </summary>
public class ShapeClickHandler
{
/// <summary>
/// Ein Action<Shape>-Delegate.
/// </summary>
public delegate void OnClickAction(Shape shape);
/// <summary>
/// Wird aufgerufen, wenn die transformierte Form geklickt wird per linker Maustaste.
/// </summary>
public event OnClickAction OnClick;
private bool isMouseOverArrow;
private bool isMouseLeftDown;
private bool isMouseLeave;
private bool isMouseLeftUp;
private readonly Shape view;
public ShapeClickHandler(Shape shape)
{
view = shape;
shape.IsHitTestVisible = true;
shape.Focusable = true;
shape.MouseEnter += onArrowEnter;
shape.MouseLeave += onArrowLeave;
shape.MouseLeftButtonDown += onArrowMouseDown;
shape.MouseLeftButtonUp += onArrowMouseUp;
}
private void onArrowEnter(object sender, MouseEventArgs e)
{
isMouseOverArrow = true;
isMouseLeave = false;
}
private void onArrowLeave(object sender, MouseEventArgs e)
{
isMouseLeave = true;
if (isMouseLeftUp)
{
isMouseOverArrow = false;
}
}
private void onArrowMouseDown(object sender, MouseButtonEventArgs e)
{
isMouseLeftUp = false;
if (isMouseOverArrow)
{
isMouseLeftDown = true;
// Unbedingt!
e.Handled = true;
}
}
private void onArrowMouseUp(object sender, MouseButtonEventArgs e)
{
if (isMouseOverArrow && !isMouseLeave && isMouseLeftDown)
{
// Klick
var handler = OnClick;
if (handler != null)
{
view.Dispatcher.Invoke(new Action(() => handler.BeginInvoke(view, null, null)));
}
}
// Auflösen!
isMouseLeftUp = true;
isMouseLeftDown = false;
if (isMouseLeave)
{
isMouseOverArrow = false;
}
}
}
Kommentare zum Snippet