Exemplo n.º 1
0
        void OnTerritoryScrollScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0)
            {
                Point?targetBefore = null;
                Point?targetNow    = null;

                if (!lastMousePositionOnTarget.HasValue)
                {
                    if (lastCenterPositionOnTarget.HasValue)
                    {
                        var centerOfViewport = new Point(TerritoryScroll.ViewportWidth / 2,
                                                         TerritoryScroll.ViewportHeight / 2);
                        Point centerOfTargetNow =
                            TerritoryScroll.TranslatePoint(centerOfViewport, TerritoryCanvas);

                        targetBefore = lastCenterPositionOnTarget;
                        targetNow    = centerOfTargetNow;
                    }
                }
                else
                {
                    targetBefore = lastMousePositionOnTarget;
                    targetNow    = Mouse.GetPosition(TerritoryCanvas);

                    lastMousePositionOnTarget = null;
                }

                if (targetBefore.HasValue)
                {
                    double dXInTargetPixels = targetNow.Value.X - targetBefore.Value.X;
                    double dYInTargetPixels = targetNow.Value.Y - targetBefore.Value.Y;

                    double multiplicatorX = e.ExtentWidth / TerritoryCanvas.Width;
                    double multiplicatorY = e.ExtentHeight / TerritoryCanvas.Height;

                    double newOffsetX = TerritoryScroll.HorizontalOffset -
                                        dXInTargetPixels * multiplicatorX;
                    double newOffsetY = TerritoryScroll.VerticalOffset -
                                        dYInTargetPixels * multiplicatorY;

                    if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY))
                    {
                        return;
                    }

                    TerritoryScroll.ScrollToHorizontalOffset(newOffsetX);
                    TerritoryScroll.ScrollToVerticalOffset(newOffsetY);
                }
            }
        }
Exemplo n.º 2
0
        void OnMouseMove(object sender, MouseEventArgs e)
        {
            Point posNow = e.GetPosition(TerritoryScroll);

            MouseLocationStatus.Text = posNow.ToString();

            if (lastDragPoint.HasValue)
            {
                if (posNow == lastDragPoint)
                {
                    return;
                }

                double dX = posNow.X - lastDragPoint.Value.X;
                double dY = posNow.Y - lastDragPoint.Value.Y;

                TerritoryScroll.ScrollToHorizontalOffset(TerritoryScroll.HorizontalOffset - dX);
                TerritoryScroll.ScrollToVerticalOffset(TerritoryScroll.VerticalOffset - dY);

                lastDragPoint = posNow;
            }
        }