/// <summary>
 /// 设置8个小正方形位置
 /// </summary>
 public void SetRectangles()
 {
     //msgBox.Text = "FocusRect height: " + FocusRect.Height + "  Width:" + FocusRect.Width + " Top:" + FocusRect.GetValue(Canvas.TopProperty) + " Left:" + FocusRect.GetValue(Canvas.LeftProperty);
     //左上
     SmallRect[0].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) - Square.Width / 2);
     SmallRect[0].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) - Square.Height / 2);
     //上中间
     SmallRect[4].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + (FocusRect.Width - Square.Width) / 2);
     SmallRect[4].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) - Square.Height / 2);
     //右上
     SmallRect[1].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + FocusRect.Width - Square.Width / 2);
     SmallRect[1].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) - Square.Height / 2);
     //左下
     SmallRect[2].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) - Square.Width / 2);
     SmallRect[2].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + FocusRect.Height - Square.Height / 2);
     //下中间
     SmallRect[5].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + (FocusRect.Width - Square.Width) / 2);
     SmallRect[5].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + FocusRect.Height - Square.Height / 2);
     //右下
     SmallRect[3].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + FocusRect.Width - Square.Height / 2);
     SmallRect[3].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + FocusRect.Height - Square.Height / 2);
     //左中间
     SmallRect[6].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) - Square.Width / 2);
     SmallRect[6].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + (FocusRect.Height - Square.Height) / 2);
     //右中间
     SmallRect[7].SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + FocusRect.Width - Square.Width / 2);
     SmallRect[7].SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + (FocusRect.Height - Square.Height) / 2);
 }
        /// <summary>
        /// 计算并设置Scroll的偏移量
        /// </summary>
        /// <param name="offSetX">鼠标X轴移动的偏移量</param>
        /// <param name="offSetY">鼠标Y轴移动的偏移量</param>
        void ComputeScrollOffSet(double offSetX, double offSetY)
        {
            double FocusRectTop     = (double)FocusRect.GetValue(Canvas.TopProperty);
            double FocusRectLeft    = (double)FocusRect.GetValue(Canvas.LeftProperty);
            double ViewportHostTop  = (double)ViewportHost.GetValue(Canvas.TopProperty);
            double ViewportHostLeft = (double)ViewportHost.GetValue(Canvas.LeftProperty);

            //msgBox.Text = "FocusRect.Height" + FocusRect.Height + "   ViewportHost.Height" + ViewportHost.Height;
            if (offSetY > 0 && (FocusRect.Height + 8) < ViewportHost.Height && (ViewportHostTop + ViewportHost.Height) > (FocusRectTop + FocusRect.Height)) //鼠标向下且未出ViewportHost区域时
            {
                imageScroll.ScrollToVerticalOffset(imageScroll.VerticalOffset + (offSetY / 2) * (Viewport.Height / (ViewportHost.Height - FocusRectTop - FocusRect.Height)));
            }

            if (offSetY < 0 && (FocusRect.Height + 8) < ViewportHost.Height && ViewportHostTop < FocusRectTop) //鼠标向上且未出ViewportHost区域时
            {
                imageScroll.ScrollToVerticalOffset(imageScroll.VerticalOffset + (offSetY / 2) * ((Viewport.Height / FocusRectTop)));
            }

            if (offSetX > 0 && (FocusRect.Width + 8) < ViewportHost.Width && (ViewportHostLeft + ViewportHost.Width) > (FocusRectLeft + FocusRect.Width)) //鼠标向右且未出ViewportHost区域时
            {
                imageScroll.ScrollToHorizontalOffset(imageScroll.HorizontalOffset + (offSetX / 2) * (Viewport.Width / (ViewportHost.Width - FocusRectLeft - FocusRect.Width)));
            }

            if (offSetX < 0 && (FocusRect.Width + 8) < ViewportHost.Width && ViewportHostLeft < FocusRectLeft) //鼠标向左且未出ViewportHost区域时
            {
                imageScroll.ScrollToHorizontalOffset(imageScroll.HorizontalOffset + (offSetX / 2) * ((Viewport.Width / FocusRectLeft)));
            }
        }
        /// <summary>
        /// 确保FocusRect在Viewport中进行缩放
        /// </summary>
        public void AssureFocusRectZoomInZone(double zoom, double mininum)
        {
            double ViewPortTop   = (double)Viewport.GetValue(Canvas.TopProperty);
            double ViewPortLeft  = (double)Viewport.GetValue(Canvas.LeftProperty);
            double FocusRectTop  = (double)FocusRect.GetValue(Canvas.TopProperty);
            double FocusRectLeft = (double)FocusRect.GetValue(Canvas.LeftProperty);

            if (zoom == mininum)
            {
                FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft);
                FocusRect.Width = Viewport.Width;
            }
            else
            {
                //确保顶部不越界
                if (ViewPortTop > FocusRectTop)
                {
                    FocusRect.SetValue(Canvas.TopProperty, ViewPortTop);
                }

                //确保左侧不越界
                if (ViewPortLeft > FocusRectLeft)
                {
                    FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft);
                }

                //判断x是否右侧越界
                if ((Viewport.Width + ViewPortLeft) < (FocusRect.Width + FocusRectLeft))
                {
                    //如果已越界,但左侧未越界
                    if (Viewport.Width > FocusRect.Width)
                    {
                        FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft + Viewport.Width - FocusRect.Width);
                    }
                    else
                    {
                        FocusRect.Width = Viewport.Width;
                    }
                }

                //判断是否底部越界
                if ((Viewport.Height + ViewPortTop) < (FocusRect.Height + FocusRectTop))
                {
                    //如果已越界,但顶部未越界
                    if (Viewport.Height > FocusRect.Height)
                    {
                        FocusRect.SetValue(Canvas.TopProperty, ViewPortTop + Viewport.Height - FocusRect.Height);
                    }
                    else
                    {
                        FocusRect.Height = Viewport.Height;
                    }
                }
            }
            SetRectangles();
        }
        /// <summary>
        /// SmallRect[]鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void smallRectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (trackingMouseMove)
            {
                FrameworkElement element = sender as FrameworkElement;
                double           offSetY = e.GetPosition(element).Y - prevLeftClick.Y;
                double           offSetX = e.GetPosition(element).X - prevLeftClick.X;

                if (AssureFocusRectMoveInZone(element.Name))
                {
                    switch (this.CurrHitPlace)
                    {
                    case HitDownSquare.HDS_TOP:
                        if ((FocusRect.Height - offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height - offSetY;
                            if (FocusRect.Height > 4)
                            {
                                FocusRect.SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + offSetY);
                            }
                        }
                        break;

                    case HitDownSquare.HDS_TOPLEFT:
                        if ((FocusRect.Height - offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height - offSetY;
                            if (FocusRect.Height > 4)
                            {
                                FocusRect.SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + offSetY);
                            }
                        }
                        if ((FocusRect.Width - offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width - offSetX;
                            if (FocusRect.Width > 4)
                            {
                                FocusRect.SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + offSetX);
                            }
                        }
                        break;

                    case HitDownSquare.HDS_TOPRIGHT:
                        if ((FocusRect.Height - offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height - offSetY;
                            if (FocusRect.Height > 4)
                            {
                                FocusRect.SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + offSetY);
                            }
                        }
                        if ((FocusRect.Width + offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width + offSetX;
                        }
                        break;

                    case HitDownSquare.HDS_RIGHT:
                        if ((FocusRect.Width + offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width + offSetX;
                        }
                        break;

                    case HitDownSquare.HDS_BOTTOM:
                        if ((FocusRect.Height + offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height + offSetY;
                        }
                        break;

                    case HitDownSquare.HDS_BOTTOMLEFT:
                        if ((FocusRect.Height + offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height + offSetY;
                        }
                        if ((FocusRect.Width - offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width - offSetX;
                            if (FocusRect.Width > 4)
                            {
                                FocusRect.SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + offSetX);
                            }
                        }
                        break;

                    case HitDownSquare.HDS_BOTTOMRIGHT:
                        if ((FocusRect.Height + offSetY) > 4)
                        {
                            FocusRect.Height = FocusRect.Height + offSetY;
                        }
                        if ((FocusRect.Width + offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width + offSetX;
                        }
                        break;

                    case HitDownSquare.HDS_LEFT:
                        if ((FocusRect.Width - offSetX) > 4)
                        {
                            FocusRect.Width = FocusRect.Width - offSetX;
                            if (FocusRect.Width > 4)
                            {
                                FocusRect.SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + offSetX);
                            }
                        }
                        break;

                    case HitDownSquare.HDS_NONE:
                        FocusRect.SetValue(Canvas.LeftProperty, (double)FocusRect.GetValue(Canvas.LeftProperty) + offSetX);
                        FocusRect.SetValue(Canvas.TopProperty, (double)FocusRect.GetValue(Canvas.TopProperty) + offSetY);
                        break;
                    }
                }
                SetRectangles();
            }
        }
        /// <summary>
        /// FocusRect是否在Viewport中,如不在,则确保其不超出Viewport区域
        /// </summary>
        /// <returns></returns>
        bool AssureFocusRectMoveInZone(string elementName)
        {
            bool result = true;
            //try
            //{
            double ViewPortTop   = (double)Viewport.GetValue(Canvas.TopProperty);
            double ViewPortLeft  = (double)Viewport.GetValue(Canvas.LeftProperty);
            double FocusRectTop  = (double)FocusRect.GetValue(Canvas.TopProperty);
            double FocusRectLeft = (double)FocusRect.GetValue(Canvas.LeftProperty);

            if (Viewport.Height > ViewportHost.Height)    //已使用放大功能,向上拖动
            {
                if (0 > FocusRectTop)
                {
                    FocusRect.SetValue(Canvas.TopProperty, (double)ViewportHost.GetValue(Canvas.TopProperty) + 4);
                    result = false;
                }
            }
            else
            {
                if (ViewPortTop > FocusRectTop)
                {
                    FocusRect.SetValue(Canvas.TopProperty, ViewPortTop);
                    result = false;
                }
            }

            if (Viewport.Width >= ViewportHost.Width)    //已使用放大功能,向左拖动
            {
                if (0 > FocusRectLeft)
                {
                    FocusRect.SetValue(Canvas.LeftProperty, (double)ViewportHost.GetValue(Canvas.LeftProperty) + 4);
                    result = false;
                }
            }
            else
            {
                if (ViewPortLeft > FocusRectLeft)
                {
                    FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft);
                    result = false;
                }
            }

            if (Viewport.Width >= ViewportHost.Width)    //已使用放大功能,向右拖动
            {
                if ((ViewportHost.Width) < (FocusRect.Width + FocusRectLeft))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.LeftProperty, ViewportHost.Width - FocusRect.Width - 4);
                    }
                    else
                    {
                        FocusRect.Width = ViewportHost.Width - FocusRectLeft - 4;
                    }
                    result = false;
                }
            }
            else
            {
                if ((Viewport.Width + ViewPortLeft) < (FocusRect.Width + FocusRectLeft))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.LeftProperty, ViewPortLeft + Viewport.Width - FocusRect.Width);
                    }
                    else
                    {
                        FocusRect.Width = ViewPortLeft + Viewport.Width - FocusRectLeft;
                    }
                    result = false;
                }
            }

            if (Viewport.Height > ViewportHost.Height)    //已使用放大功能,向下拖动
            {
                if ((ViewportHost.Height) < (FocusRect.Height + FocusRectTop))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.TopProperty, ViewportHost.Height - FocusRect.Height - 4);
                    }
                    else
                    {
                        FocusRect.Height = ViewportHost.Height - FocusRectTop - 4;
                    }
                    result = false;
                }
            }
            else
            {
                if ((Viewport.Height + ViewPortTop) < (FocusRect.Height + FocusRectTop))
                {
                    if (elementName == "FocusRect")
                    {
                        FocusRect.SetValue(Canvas.TopProperty, ViewPortTop + Viewport.Height - FocusRect.Height);
                    }
                    else
                    {
                        FocusRect.Height = ViewPortTop + Viewport.Height - FocusRectTop;
                    }
                    result = false;
                }
            }

            //}
            //catch
            //{
            //    result = false;
            //}
            return(result);
        }
        /// <summary>
        /// FocusRect鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FocusRect_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            if (element != null)
            {
                element.Cursor = Cursors.None;
            }

            if (trackingMouseMove)
            {
                double offSetX = e.GetPosition(element).X - prevLeftClick.X;
                double offSetY = e.GetPosition(element).Y - prevLeftClick.Y;

                if (((double)element.GetValue(Canvas.TopProperty) + offSetY) >= 4 && (((double)FocusRect.GetValue(Canvas.TopProperty) + FocusRect.Height) + offSetY + 3) <= ViewportHost.Height)
                {
                    element.SetValue(Canvas.TopProperty, (double)element.GetValue(Canvas.TopProperty) + offSetY);
                }

                if (((double)element.GetValue(Canvas.LeftProperty) + offSetX) >= 4 && (((double)FocusRect.GetValue(Canvas.LeftProperty) + FocusRect.Width) + offSetX + 3) <= ViewportHost.Width)
                {
                    element.SetValue(Canvas.LeftProperty, (double)element.GetValue(Canvas.LeftProperty) + offSetX);
                }

                ComputeScrollOffSet(offSetX, offSetY);
                SetRectangles();
            }
            customCursors.SetPostion(e.GetPosition(LayoutRoot));
        }