コード例 #1
0
        private void FormChooseWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if (canfind && !rectChoosed)
            {
                canfind = false;
                //移动鼠标根据鼠标位置找窗口
                //百度来的 ChildWindowFromPointEx
                IntPtr thisfind = API.ChildWindowFromPointEx(API.GetDesktopWindow(), new API.POINT(e.X, e.Y),
                                                             0x0004 | 0x0001 | 0x0002 /*(CWP_SKIPTRANSPARENT )表示忽略透明窗口,而本窗口设置  标识则可忽略本窗口*/);
                //上一个找到的不等于这个找到的
                if (lastFindedWnd != thisfind)
                {
                    //重绘框
                    lastFindedWnd = thisfind;
                    API.GetWindowRect(lastFindedWnd, out lastFindedWndRect);
                    formColorPick.RectSizeStr = lastFindedWndRect.Width + " x " + lastFindedWndRect.Height;
                    Invalidate();
                }
            }
            if (!rectChoosed || mouseDowned)
            {
                Point p  = MousePosition;
                Point tp = new Point();
                if (p.X > screenWidth - formColorPick.Width - 25)
                {
                    tp.X = p.X - formColorPick.Width - 25;
                }
                else
                {
                    tp.X = p.X + 25;
                }
                if (p.Y > screenHeight - formColorPick.Height - 25)
                {
                    tp.Y = p.Y - formColorPick.Height - 25;
                }
                else
                {
                    tp.Y = p.Y + 25;
                }

                formColorPick.Location = tp;
                formColorPick.InvalidatePreview();
                WindowUtils.Top(formColorPick.Handle);
            }
            if (mouseDowned)
            {
                //拖动框
                if (!rectChoosed) //拉出一个框
                {
                    pointEnd = MousePosition;
                    //重置绘画的矩形框
                    if (pointStart.X > pointEnd.X)
                    {
                        lastFindedWndRect.Left  = pointEnd.X;
                        lastFindedWndRect.Right = pointStart.X;
                    }
                    else
                    {
                        lastFindedWndRect.Left  = pointStart.X;
                        lastFindedWndRect.Right = pointEnd.X;
                    }
                    if (pointStart.Y > pointEnd.Y)
                    {
                        lastFindedWndRect.Top    = pointEnd.Y;
                        lastFindedWndRect.Bottom = pointStart.Y;
                    }
                    else
                    {
                        lastFindedWndRect.Top    = pointStart.Y;
                        lastFindedWndRect.Bottom = pointEnd.Y;
                    }
                    Invalidate();
                }
                else if (formEditScreenShutTools.CurrentTools == PaintTools.None) //拖动调整框大小
                {
                    if (currentSizeType == SizeType.Move)
                    {
                        int   w  = lastFindedWndRect.Width;
                        int   h  = lastFindedWndRect.Height;
                        Point tp = new Point(MousePosition.X - pointStartRefRect.X,
                                             MousePosition.Y - pointStartRefRect.Y);
                        if (tp.X < 0)
                        {
                            tp.X = 0;
                        }
                        if (tp.Y < 0)
                        {
                            tp.Y = 0;
                        }
                        if (tp.X + w > screenWidth)
                        {
                            tp.X = screenWidth - w;
                        }
                        if (tp.Y + h > screenHeight)
                        {
                            tp.Y = screenHeight - h;
                        }
                        lastFindedWndRect.Left   = tp.X;
                        lastFindedWndRect.Top    = tp.Y;
                        lastFindedWndRect.Right  = tp.X + w;
                        lastFindedWndRect.Bottom = tp.Y + h;
                        ResetToolPosition();
                        Invalidate();
                    }
                    else
                    {
                        bool resized = false;

                        if ((currentSizeType & SizeType.Top) == SizeType.Top)
                        {
                            Point tp = MousePosition;
                            if (tp.Y < 0)
                            {
                                tp.Y = 0;
                            }
                            else if (tp.Y > lastFindedWndRect.Bottom)
                            {
                                currentSizeType ^= SizeType.Top;
                                currentSizeType |= SizeType.Buttom;
                            }
                            lastFindedWndRect.Top = tp.Y;
                            resized = true;
                        }
                        if ((currentSizeType & SizeType.Buttom) == SizeType.Buttom)
                        {
                            Point tp = MousePosition;
                            if (tp.Y > screenHeight)
                            {
                                tp.Y = screenHeight;
                            }
                            else if (tp.Y < lastFindedWndRect.Top)
                            {
                                currentSizeType ^= SizeType.Buttom;
                                currentSizeType |= SizeType.Top;
                            }
                            lastFindedWndRect.Bottom = tp.Y;
                            resized = true;
                        }

                        if ((currentSizeType & SizeType.Left) == SizeType.Left)
                        {
                            Point tp = MousePosition;
                            if (tp.X < 0)
                            {
                                tp.X = 0;
                            }
                            else if (tp.X > lastFindedWndRect.Right)
                            {
                                currentSizeType ^= SizeType.Left;
                                currentSizeType |= SizeType.Right;
                            }
                            lastFindedWndRect.Left = tp.X;
                            resized = true;
                        }
                        if ((currentSizeType & SizeType.Right) == SizeType.Right)
                        {
                            Point tp = MousePosition;
                            if (tp.X > screenWidth)
                            {
                                tp.X = screenWidth;
                            }
                            else if (tp.X < lastFindedWndRect.Left)
                            {
                                currentSizeType ^= SizeType.Right;
                                currentSizeType |= SizeType.Left;
                            }
                            lastFindedWndRect.Right = tp.X;
                            resized = true;
                        }

                        if (resized)
                        {
                            ResetToolPosition();
                            formColorPick.InvalidatePreview();
                            formColorPick.RectSizeStr = lastFindedWndRect.Width + " x " + lastFindedWndRect.Height;
                            Invalidate();
                        }
                    }
                }
                else //画板
                {
                    //绘制工具
                    paintBox.OnMouseMove(e);
                }
            }
            else if (rectChoosed)
            {
                //检测鼠标是否在框的四角
                Rectangle rc = new Rectangle(lastFindedWndRect.Left - 10, lastFindedWndRect.Top - 10,
                                             lastFindedWndRect.Right - lastFindedWndRect.Left + 20, lastFindedWndRect.Bottom - lastFindedWndRect.Top + 20);
                Point pt2 = new Point(e.X - rc.Left, e.Y - rc.Top);
                //设置鼠标指针
                if (pt2.X > 0 && pt2.X < SIZE_BORDER && pt2.Y > SIZE_BORDER && pt2.Y < rc.Height - SIZE_BORDER)
                {
                    Cursor          = Cursors.SizeWE;
                    currentSizeType = SizeType.Left;
                }
                else if (pt2.Y > 0 && pt2.Y < SIZE_BORDER && pt2.X > SIZE_BORDER && pt2.X < rc.Width - SIZE_BORDER)
                {
                    Cursor          = Cursors.SizeNS;
                    currentSizeType = SizeType.Top;
                }
                else if (pt2.Y > rc.Height - SIZE_BORDER && pt2.Y < rc.Height && pt2.X > SIZE_BORDER && pt2.X < rc.Width - SIZE_BORDER)
                {
                    Cursor          = Cursors.SizeNS;
                    currentSizeType = SizeType.Buttom;
                }
                else if (pt2.X > rc.Width - SIZE_BORDER && pt2.X < rc.Width && pt2.Y > SIZE_BORDER && pt2.Y < rc.Height - SIZE_BORDER)
                {
                    Cursor          = Cursors.SizeWE;
                    currentSizeType = SizeType.Right;
                }
                else if (pt2.X > 0 && pt2.X < SIZE_BORDER_2 && pt2.Y < SIZE_BORDER_2 && pt2.Y > 0)
                {
                    Cursor          = Cursors.SizeNWSE;
                    currentSizeType = SizeType.TopLeft;
                }
                else if (pt2.X > rc.Width - SIZE_BORDER_2 && pt2.X < rc.Width && pt2.Y < SIZE_BORDER_2 && pt2.Y > 0)
                {
                    Cursor.Current  = Cursors.SizeNESW;
                    currentSizeType = SizeType.TopRight;
                }
                else if (pt2.Y > rc.Height - SIZE_BORDER_2 && pt2.Y < rc.Height && pt2.X > rc.Width - SIZE_BORDER_2 && pt2.X < rc.Width)
                {
                    Cursor          = Cursors.SizeNWSE;
                    currentSizeType = SizeType.ButtomRight;
                }
                else if (pt2.X < SIZE_BORDER_2 && pt2.X > 0 && pt2.Y > rc.Height - SIZE_BORDER_2 && pt2.Y < rc.Height)
                {
                    Cursor          = Cursors.SizeNESW;
                    currentSizeType = SizeType.ButtomLeft;
                }
                else if (pt2.X > SIZE_BORDER_2 && pt2.Y > SIZE_BORDER_2 &&
                         pt2.X < rc.Width - SIZE_BORDER_2 && pt2.Y < rc.Height - SIZE_BORDER_2)
                {
                    if (formEditScreenShutTools.CurrentTools == PaintTools.None)
                    {
                        Cursor = Cursors.SizeAll;
                    }
                    else
                    {
                        Cursor = formEditScreenShutTools.cur_current;
                    }
                    currentSizeType = SizeType.Move;
                }
                else
                {
                    Cursor          = formEditScreenShutTools.cur_default;
                    currentSizeType = SizeType.None;
                }
            }
        }