Вопрос

Ok so far i have

private void UserControl_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Up)
            this.Pin.Move(Direction.Up);
        if (e.Key == Key.Down)
            this.Pin.Move(Direction.Down);
        if (e.Key == Key.Left)
            this.Pin.Move(Direction.Left);
        if (e.Key == Key.Right)
            this.Pin.Move(Direction.Right);
    }

It is great, i can move my object with up down left and right. I would like to control it with my souse cursor though. any idea how i would do this. Im not good at this and have just started some pointers will do.

Это было полезно?

Решение

I suspect that you want to override the OnMouseMove method for your UserControl. You can then look at the MouseEventArgs which you get from that handler and use the GetPosition method to get the mouse co-ordinate.

If you stored this point in a local varaible somewhere, the after the first mouse move you could compare it and move it in the appropriate direction. An example bit of code might be

private Point prev;

private void UserControl_OnMouseMove(object sender, MouseEventArgs e)
{
    Point p = e.GetPosition();
    if(prev == null)
       prev = p;

    if(p.Y > prev.Y) 
         this.Pin.Move(Direction.Up);
    else if(p.Y < prev.Y) 
          this.Pin.Move(Direction.Down);
    etc. 
    prev = p;
}

Hope this helps

Другие советы

The following might help you with modification to fit your implementation

private bool _isMouseDown;

  private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isMouseDown)
            {
                this.Pin.Location = new Point(e.X,e.Y);
            }
        }

        private void UserControl_MouseDown(object sender, MouseEventArgs e)
        {

            _isMouseDown = true;
        }
        private void UserControl_MouseUp(object sender, MouseEventArgs e)
        {
            _isMouseDown = false;
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top