private static UnityEngine.Rect CheckWindowSize(UnityEngine.Rect windowRect)
        {
            if (windowRect.width < MinWindowSize.x)
            {
                windowRect.width = MinWindowSize.x;
            }

            if (windowRect.height < MinWindowSize.y)
            {
                windowRect.height = MinWindowSize.y;
            }

            if (MaxWindowSize.x != 0)
            {
                if (windowRect.width > MaxWindowSize.x)
                {
                    windowRect.width = MaxWindowSize.x;
                }
            }

            if (MaxWindowSize.y != 0)
            {
                if (windowRect.height > MaxWindowSize.y)
                {
                    windowRect.height = MaxWindowSize.y;
                }
            }

            return(windowRect);
        }
        public static UnityEngine.Rect GetWindowRect()
        {
            var activeWindow = User32.GetActiveWindow();

            User32.GetWindowRect(activeWindow, out var windowRect);

            var rect = new UnityEngine.Rect
            {
                x      = windowRect.Left,
                y      = windowRect.Top,
                width  = windowRect.Right - windowRect.Left,
                height = windowRect.Bottom - windowRect.Top
            };

            return(rect);
        }
        public static void MoveWindow(UnityEngine.Rect rect, bool checkLimits)
        {
            SetWindowPos(rect, checkLimits);
            return;

            var activeWindow = User32.GetActiveWindow();

            if (checkLimits)
            {
                rect = CheckWindowSize(rect);
            }

            var x      = (int)rect.x;
            var y      = (int)rect.y;
            var width  = (int)rect.width;
            var height = (int)rect.height;


            User32.MoveWindow(activeWindow, x, y, width, height, true);
        }
        public static void SetWindowPos(UnityEngine.Rect rect, bool checkLimits)
        {
            var activeWindow = User32.GetActiveWindow();

            if (checkLimits)
            {
                rect = CheckWindowSize(rect);
            }

            var x      = (int)rect.x;
            var y      = (int)rect.y;
            var width  = (int)rect.width;
            var height = (int)rect.height;

//        UpdateWindowStyle();

            const uint message = (int)SetWindowPosFlags.DeferBase |
                                 (int)SetWindowPosFlags.AsyncWindowPos;


//        WinApi.DeferWindowPos(deferWindow, activeWindow, IntPtr.Zero, x, y, width, height, message);
            User32.SetWindowPos(activeWindow, 0, x, y, width, height, message);
//
//        var rectt = new RECT();
//        rectt.X = x;
//        rectt.Y = y;
//        rectt.Width = width;
//        rectt.Height = height;
//
//        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(rectt));
//        Marshal.StructureToPtr(rectt, pnt, true);
//
//        WinApi.SendMessage(activeWindow, (int) WindowMessages.SIZING, 1, pnt);
//
//        Marshal.FreeHGlobal(pnt);
        }
        private static UnityEngine.Rect WindowRectResizeLeft(Vector2 beginningCursorEdgeDistance, Rect windowRectPoints,
                                                             Vector2 globalCursorPosition, UnityEngine.Rect newWindowRect)
        {
            var globalClickPoint = windowRectPoints.Left + beginningCursorEdgeDistance.x;
            var distance         = Mathf.Abs(globalCursorPosition.x - globalClickPoint);

            //Left Expand
            if (globalClickPoint > globalCursorPosition.x)
            {
                if (MaxWindowSize.x == 0)
                {
                    newWindowRect.x     -= distance;
                    newWindowRect.width += distance;
                }
                else if (newWindowRect.width < MaxWindowSize.x)
                {
                    newWindowRect.x     -= distance;
                    newWindowRect.width += distance;
                }

                if (newWindowRect.width > MaxWindowSize.x)
                {
                    var difference = newWindowRect.width - MaxWindowSize.x;
                    newWindowRect.x += difference;
                }
            }

            //Left Shrink
            else if (globalClickPoint < globalCursorPosition.x)
            {
                if (newWindowRect.width > MinWindowSize.x)
                {
                    newWindowRect.x     += distance;
                    newWindowRect.width -= distance;
                }

                if (newWindowRect.width < MinWindowSize.x)
                {
                    var difference = MinWindowSize.x - newWindowRect.width;
                    newWindowRect.x -= difference;
                }
            }

            return(newWindowRect);
        }
        private static UnityEngine.Rect WindowRectResizeRight(Vector2 beginningCursorEdgeDistance,
                                                              Rect windowRectPoints,
                                                              Vector2 globalCursorPosition, UnityEngine.Rect newWindowRect)
        {
            var globalClickPoint = windowRectPoints.Right - beginningCursorEdgeDistance.x;
            var distance         = Mathf.Abs(globalCursorPosition.x - globalClickPoint);

            //Right Expand
            if (globalClickPoint > globalCursorPosition.x)
            {
                newWindowRect.width -= distance;
            }
            //Right Shrink
            else if (globalClickPoint < globalCursorPosition.x)
            {
                newWindowRect.width += distance;
            }

            return(newWindowRect);
        }
        private static UnityEngine.Rect WindowRectResizeTop(Vector2 beginningCursorEdgeDistance, Rect windowRectPoints,
                                                            Vector2 globalCursorPosition, UnityEngine.Rect newWindowRect)
        {
            var globalClickPoint = windowRectPoints.Top + beginningCursorEdgeDistance.y;
            var distance         = Mathf.Abs(globalCursorPosition.y - globalClickPoint);

            //Top Expand
            if (globalClickPoint > globalCursorPosition.y)
            {
//            newWindowRect.y -= distance;
//            newWindowRect.height += distance;

                if (MaxWindowSize.y == 0)
                {
                    newWindowRect.y      -= distance;
                    newWindowRect.height += distance;
                }
                else if (newWindowRect.height < MaxWindowSize.y)
                {
                    newWindowRect.y      -= distance;
                    newWindowRect.height += distance;
                }

                if (newWindowRect.height > MaxWindowSize.y)
                {
                    var difference = newWindowRect.height - MaxWindowSize.y;
                    newWindowRect.y += difference;
                }
            }
            //Top Shrink
            else if (globalClickPoint < globalCursorPosition.y)
            {
//            newWindowRect.y += distance;
//            newWindowRect.height -= distance;

                if (newWindowRect.height > MinWindowSize.y)
                {
                    newWindowRect.y      += distance;
                    newWindowRect.height -= distance;
                }

                if (newWindowRect.height < MinWindowSize.y)
                {
                    var difference = MinWindowSize.y - newWindowRect.height;
                    newWindowRect.y -= difference;
                }
            }

            return(newWindowRect);
        }
        private static UnityEngine.Rect WindowRectResizeBottom(Vector2 beginningCursorEdgeDistance,
                                                               Rect windowRectPoints,
                                                               Vector2 globalCursorPosition, UnityEngine.Rect newWindowRect)
        {
            var globalClickPoint = windowRectPoints.Bottom - beginningCursorEdgeDistance.y;
            var distance         = Mathf.Abs(globalCursorPosition.y - globalClickPoint);

            //Bottom Expand
            if (globalClickPoint > globalCursorPosition.y)
            {
                newWindowRect.height -= distance;
            }
            //Bottom Shrink
            else if (globalClickPoint < globalCursorPosition.y)
            {
                newWindowRect.height += distance;
            }

            return(newWindowRect);
        }