示例#1
0
        public static NM.tagPoint MouseMove(IntPtr Handle, int x, int y, bool PerformCheck = true)
        {
            NM.tagRect  WindowRect;
            NM.tagRect  ClientRect;
            NM.tagPoint thePoint;
            int         xOffset;
            int         yOffset;

            Stopwatch timer = Stopwatch.StartNew();

            while (true)
            {
                NM.GetWindowRect(Handle, out WindowRect);
                NM.GetClientRect(Handle, out ClientRect);

                //TODO fix this as -1 might be a valid move,,, maybe 0 instead or...
                if (x == -1)
                {
                    xOffset = ClientRect.right / 2;
                }
                else
                {
                    xOffset = x;
                }

                if (y == -1)
                {
                    yOffset = ClientRect.bottom / 2;
                }
                else
                {
                    yOffset = y;
                }

                //Convert the window area to screen point
                thePoint.x = WindowRect.left + xOffset;
                thePoint.y = WindowRect.top + yOffset;

                if (NM.MonitorFromPoint(thePoint, NM.MonitorOptions.MONITOR_DEFAULTTONULL) == null)
                {
                    throw new Exception("coordinate appears to be offscreen");
                }

                if (PerformCheck)
                {
                    IntPtr ChildHandle;

                    thePoint.x = xOffset + WindowRect.left;
                    thePoint.y = yOffset + WindowRect.top;

                    ChildHandle = NM.WindowFromPoint(thePoint);

                    //Make sure we are inside the controls window area
                    if (Handle == ChildHandle)
                    {
                        break;
                    }
                    else
                    {
                        //Try to scroll it into view
                        GUI.m_APE.AddFirstMessageScrollControlIntoView(Handle);
                        GUI.m_APE.SendMessages(EventSet.APE);
                        GUI.m_APE.WaitForMessages(EventSet.APE);

                        if (timer.ElapsedMilliseconds > GUI.GetTimeOut())
                        {
                            throw new Exception("Coordinates are not inside the controls area");
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            //Get the current mouse location
            NM.tagPoint currentPoint;
            NM.GetCursorPos(out currentPoint);

            //X direction
            int DirectionX;

            if (currentPoint.x <= WindowRect.left + xOffset)
            {
                DirectionX = 1;     //right
            }
            else
            {
                DirectionX = -1;    //left
            }

            //Y direction
            int DirectionY;

            if (currentPoint.y <= WindowRect.top + yOffset)
            {
                DirectionY = 1;     //down
            }
            else
            {
                DirectionY = -1;    //up
            }

            int MoveX = currentPoint.x;
            int MoveY = currentPoint.y;

            while (MoveX != WindowRect.left + xOffset || MoveY != WindowRect.top + yOffset)
            {
                if (MoveX != WindowRect.left + xOffset)
                {
                    if (DirectionX == 1)
                    {
                        if (MoveX + MoveSize > WindowRect.left + xOffset)
                        {
                            MoveX = MoveX + 1;
                        }
                        else
                        {
                            MoveX = MoveX + MoveSize;
                        }
                    }
                    else
                    {
                        if (MoveX - MoveSize < WindowRect.left + xOffset)
                        {
                            MoveX = MoveX - 1;
                        }
                        else
                        {
                            MoveX = MoveX - MoveSize;
                        }
                    }
                }

                if (MoveY != WindowRect.top + yOffset)
                {
                    if (DirectionY == 1)
                    {
                        if (MoveY + MoveSize > WindowRect.top + yOffset)
                        {
                            MoveY = MoveY + 1;
                        }
                        else
                        {
                            MoveY = MoveY + MoveSize;
                        }
                    }
                    else
                    {
                        if (MoveY - MoveSize < WindowRect.top + yOffset)
                        {
                            MoveY = MoveY - 1;
                        }
                        else
                        {
                            MoveY = MoveY - MoveSize;
                        }
                    }
                }

                MoveMouse(MoveX, MoveY);
            }

            return(thePoint);
        }