Example #1
0
        private void imageCroppingBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_ptCurrent == e.Location)
            {
                return;
            }
            m_ptCurrent = e.Location;
            if (imageCroppingBox1.IsLockSelected)                       //如果已经锁定了选取 则有可能是正在进行后期绘制
            {
                imageCroppingBox1.Cursor = imageCroppingBox1.SelectedRectangle.Contains(e.Location) ? Cursors.Cross : Cursors.Default;
                if (m_bDrawEffect)
                {
                    this.DrawEffects();                                                //如果说在mousedown中设置了绘制标识 那么则表示是在进行后期绘制
                }
                else if (imageCroppingBox1.SelectedRectangle.Contains(e.Location))     //如果没有设置绘制标识 判断鼠标是否在选取内
                {
                    imageCroppingBox1.Invalidate(imageCroppingBox1.SelectedRectangle); //重绘选取 以便在paint绘制画笔大小预览(圆和矩形的那个)
                }
                return;
            }//如果已经锁定选取 那么直接return掉
            //在mousemove中还有可能是正在根据鼠标位置自动框选窗体 以下代码为获取窗体信息
            string strText      = string.Empty;
            string strClassName = string.Empty;

            m_bDrawInfo = !(imageCroppingBox1.IsSelected || e.Button != MouseButtons.None);
            if (!m_bDrawInfo)
            {
                return;                                                 //如果已经有选取 或者移动过程中有鼠标被点下 都不需要获取窗体信息了
            }
            IntPtr hWnd = Win32.GetWindowFromPoint(e.Location.X, e.Location.Y, this.Handle, m_bGetVisable, m_bGetTransparent);

            m_hWnd       = hWnd;                                                //更具鼠标位置获取窗体句柄
            strClassName = Win32.GetClassName(hWnd);
            strText      = Win32.GetWindowText(hWnd);
            Win32.RECT rect = new Win32.RECT();
            Win32.GetWindowRect(hWnd, ref rect);                                   //获取窗体大小
            m_rect = rect.ToRectangle();
            if (m_bSpyWeb && strClassName.ToLower() == "internet explorer_server") //如果说当前窗体是webbrowser控件 并且有设置进行spy
            {
                var element = Win32.GetHtmlDocument(hWnd).elementFromPoint(e.Location.X - rect.Left, e.Location.Y - rect.Top);
                if (element != null)
                {
                    var p = ((mshtml.IHTMLElement2)element).getBoundingClientRect();        //获取当前鼠标下的html元素信息
                    m_rect.X      = rect.Left + p.left;
                    m_rect.Y      = rect.Top + p.top;
                    m_rect.Width  = p.right - p.left;
                    m_rect.Height = p.bottom - p.top;
                    string strInner = element.innerText == null ? "null" : element.innerText.Trim().Split('\n')[0];
                    strText = string.Format("<{0}>innerText:[{1}]", element.tagName, strInner + (strInner.Length == 1 ? "" : "..."));
                    if (strText.Length > 100)
                    {
                        strText = strText.Substring(0, 100) + "...]";
                    }
                }
            }
            m_strShow = string.Format("TEXT:{0}\r\nHWND:0x{1} [{2}]", strText, hWnd.ToString("X").PadLeft(8, '0'), strClassName);
            if (m_rectScreen.X < 0)
            {
                m_rect.X -= m_rectScreen.X;                         //判断一下屏幕的坐标是否是小于0的 如果是 则在绘制区域的时候需要加上这个差值
            }
            if (m_rectScreen.Y < 0)
            {
                m_rect.Y -= m_rectScreen.Y;                         //多显示器屏幕坐标可能是负数{如(-1920,0)两个显示器 主显示器在右边} 在绘制的时候窗体内部坐标是0开始的 需要转换
            }
            m_rect.Intersect(imageCroppingBox1.DisplayRectangle);   //确定出来的区域和截图窗体的区域做一个交集 (不能让区域超出屏幕啊 比如屏幕边缘有个窗体 那么框选出来的区域是超出屏幕的)
            imageCroppingBox1.Invalidate();
        }