Google Search

Monday, April 6, 2009

Creating customizable WPF drop-down menus (2)


Step 2: Detecting drag


Drag will start when the user had pressed the mouse button on an item and moved it enough distance in pixels while being pressed.
So, we need to know where the mouse was pressed, then track it all time it is pressed until it moves far enough:
public class DraggableMenuItem : MenuItem
{
    private Point m_dragStartPoint;

    public static  bool IsDragging { get; set; }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        m_dragStartPoint = e.GetPosition(this);
        base.OnMouseLeftButtonDown(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.LeftButton != MouseButtonState.Pressed || IsDragging)
            return;

        Point p = e.GetPosition(this);
        if (Point.Subtract(m_dragStartPoint, p).LengthSquared < 16)
        {
            e.Handled = true;
            return;
        }

        DoDragging();
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        IsDragging = false;
        base.OnMouseLeftButtonUp(e);
    }

    private void DoDragging()
    {
    }
}
Note that the IsDragging flag is a static flag, thus shared among all instances of the DraggableMenuItem. In the code sample above we do not yet assign it. This will be done in the next steps, in addition to some other places we will need to use its value to achieve the correct behavior.

1 comment:

  1. You can moreover discover websites that offer on line casino cashback funds based on your losses encountered while half in} with a number of} on-line casinos. Those forms of cashback offers are normally paid again to gamers by the on line casino portal that gives these particular cashback presents. You can play on-line slots for real money with confidence 1xbet korea that your data and money is safe, lengthy as|so lengthy as} you sign up with licensed and controlled on-line casinos in Canada. Canadian on-line casinos with the correct licensing utilize trustworthy and safe banking choices to ensure that|to make sure that} your money doesn't go to any unsolicited entities. Almost each on-line on line casino nowadays has slot machines in its game library.

    ReplyDelete