C# WPF VisualBrush实现鹰眼视图

        private Rectangle HawkeyeBack { get; set; }

        private Grid HawEyeBox { get; set; }

        private Rectangle EyeWindow { get; set; }


        #region 鹰眼视窗

        /// <summary>
        /// 初始化鹰眼视窗
        /// </summary>
        void HawkeyeInit()
        {
            VisualBrush myVisualBrush = new VisualBrush();
            EyeWindow = new Rectangle();
            HawEyeBox = new Grid();
            myVisualBrush.Visual = Diagram1;
            HawEyeBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            HawEyeBox.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            HawEyeBox.Width = 300;
            HawEyeBox.Height = 200;
            EyeWindow.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            EyeWindow.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            EyeWindow.Width = 300;
            EyeWindow.Height = 200;
            EyeWindow.Stroke = Brushes.White;
            HawkeyeBack = new Rectangle();
            HawkeyeBack.Width = Double.NaN;
            HawkeyeBack.Height = Double.NaN;
            HawkeyeBack.Stroke = Brushes.Yellow;
            HawkeyeBack.Margin = new Thickness(0, 0, 0, 0);
            HawkeyeBack.Fill = myVisualBrush;
            HawkeyeBack.MouseMove += myRectangle_MouseMove;
            HawkeyeBack.MouseDown += myRectangle_MouseDown;
            HawkeyeBack.MouseUp += HawkeyeBack_MouseUp;
            HawEyeBox.Children.Add(HawkeyeBack);
            HawEyeBox.Children.Add(EyeWindow);
            grid.Children.Add(HawEyeBox);
        }

        void myRectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle Sender = sender as Rectangle;
            //标记下鼠标按下
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Sender.Tag = new object();
                myRectangle_MouseMove(sender, e);
            }
        }

        private void HawkeyeBack_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Rectangle Sender = sender as Rectangle;
            //去掉鼠标按下的标记
            Sender.Tag = null;

            b_lbuttondown = false;
        }

        /// <summary>
        /// 鼠标操作视窗 移动 定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void myRectangle_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle Sender = sender as Rectangle;
            Point P = e.GetPosition(Sender);
            Double ewx = 0;
            Double ewy = 0;
            if (Sender.Tag != null)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    //计算鹰眼框位置
                    {
                        ewx = P.X - EyeWindow.Width / 2;
                        ewy = P.Y - EyeWindow.Height / 2;
                        ewx = ewx < 0 ? 0 : ewx;
                        ewy = ewy < 0 ? 0 : ewy;

                        ewx = ewx + EyeWindow.Width > HawEyeBox.Width ? HawEyeBox.Width - EyeWindow.Width : ewx;
                        ewy = ewy + EyeWindow.Height > HawEyeBox.Height ? HawEyeBox.Height - EyeWindow.Height : ewy;

                        EyeWindow.Margin = new Thickness(Double.IsNaN(ewx) ? 0 : Math.Round(ewx), Double.IsNaN(ewy) ? 0 : Math.Round(ewy), 0, 0);
                    }

                    //移动视窗到指定位置
                    {
                        Double secx = ewx / (HawEyeBox.Width - EyeWindow.Width);
                        Double secy = ewy / (HawEyeBox.Height - EyeWindow.Height);
                        var X = scr.ScrollableWidth * secx;
                        var Y = scr.ScrollableHeight * secy;
                        scr.ScrollToHorizontalOffset(Double.IsNaN(X) ? 0 : X);
                        scr.ScrollToVerticalOffset(Double.IsNaN(Y) ? 0 : Y);
                    }
                }
            }
            else
            {
                if (b_lbuttondown)
                {
                    Diagram1_MouseMove(Diagram1, e);
                }
            }
        }

        /// <summary>
        /// 更新视窗大小和位置
        /// </summary>
        private void UpDateHawEyeWindow()
        {
            //更新鹰眼视窗大小
            Double scale_x = Diagram1.ActualWidth / scr.ActualWidth;
            Double scale_y = Diagram1.ActualHeight / scr.ActualHeight;
            EyeWindow.Width = Math.Round(HawEyeBox.Width / scale_x);
            EyeWindow.Height = Math.Round(HawEyeBox.Height / scale_y);

            EyeWindow.Width = EyeWindow.Width > HawEyeBox.Width ? HawEyeBox.Width : EyeWindow.Width;
            EyeWindow.Height = EyeWindow.Height > HawEyeBox.Height ? HawEyeBox.Height : EyeWindow.Height;

            //定位鹰眼视窗
            var X = (HorizontalOffset / scr.ScrollableWidth) * (HawEyeBox.Width - EyeWindow.Width);
            var Y = (VerticalOffset / scr.ScrollableHeight) * (HawEyeBox.Height - EyeWindow.Height);
            EyeWindow.Margin = new Thickness(Double.IsNaN(X) ? 0 : Math.Round(X), Double.IsNaN(Y) ? 0 : Math.Round(Y), 0, 0);

            //EyeWindow.UpdateLayout();

        }
        #endregion

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值