コード例 #1
0
        public MouseDragAction(IntPtr hwnd, Point initialMousePosition)
        {
            mHwnd = hwnd;
            mInitialMousePosition = initialMousePosition;

            var windowPlacement = WinAPI.GetPlacement(hwnd);

            if (windowPlacement == WinAPI.ShowWindowCommands.SW_MAXIMIZE)
            {
                WinAPI.ShowWindow(hwnd, WinAPI.ShowWindowCommands.SW_RESTORE);
            }

            if (!WinAPI.GetWindowRect(hwnd, out var windowRect))
            {
                throw new InvalidOperationException(
                          $"Could not get window rect of window {hwnd}");
            }

            mInitialWindowPosition = new Point(windowRect.Left, windowRect.Top);

            mInvisibleWindow = new InvisibleWindow();
            mInvisibleWindow.CenterAt(initialMousePosition);
            mInvisibleWindow.Cursor = Cursors.Hand;
            mInvisibleWindow.Show();

            mSnapRectangle = new SnapRectangle()
            {
                Visible = false
            };
        }
コード例 #2
0
        public MouseResizeAction(IntPtr hwnd, Point initialMousePosition)
        {
            mInitialMousePosition = initialMousePosition;
            mHwnd = hwnd;

            if (!WinAPI.GetWindowRect(hwnd, out var rect))
            {
                throw new InvalidOperationException($"Could not get window rect of window {hwnd}");
            }

            var cornerSize = Properties.Settings.Default.sideWidth / 2;
            var sideSize   = cornerSize;

            mInitialWindowRect = mNewWindowRect =
                new Rectangle(rect.Left, rect.Top,
                              rect.Right - rect.Left,
                              rect.Bottom - rect.Top);

            var normalizedMousePos = new PointF(
                (initialMousePosition.X - mInitialWindowRect.X)
                / (float)mInitialWindowRect.Width,
                (initialMousePosition.Y - mInitialWindowRect.Y)
                / (float)mInitialWindowRect.Height);

            var areas = new Dictionary <ResizeMode, RectangleF> {
                [ResizeMode.TopLeft]     = new RectangleF(0, 0, cornerSize, cornerSize),
                [ResizeMode.TopRight]    = new RectangleF(1 - cornerSize, 0, cornerSize, cornerSize),
                [ResizeMode.BottomLeft]  = new RectangleF(0, 1 - cornerSize, cornerSize, cornerSize),
                [ResizeMode.BottomRight] = new RectangleF(1 - cornerSize, 1 - cornerSize, cornerSize, cornerSize),

                [ResizeMode.Top]    = new RectangleF(0, 0, 1, sideSize),
                [ResizeMode.Left]   = new RectangleF(0, 0, sideSize, 1),
                [ResizeMode.Right]  = new RectangleF(1 - sideSize, 0, sideSize, 1),
                [ResizeMode.Bottom] = new RectangleF(0, 1 - sideSize, 1, sideSize),
            };

            WinAPI.SendMessageW(hwnd, WinAPI.WM_ENTERSIZEMOVE, IntPtr.Zero, IntPtr.Zero);

            if (areas.TryFirstOrDefault(kvp => kvp.Value.Contains(normalizedMousePos), out var areaKvp))
            {
                mResizeMode = areaKvp.Key;
            }
            else
            {
                mResizeMode = ResizeMode.All;
            }

            mInvisibleWindow = new InvisibleWindow();
            mInvisibleWindow.CenterAt(initialMousePosition);
            mInvisibleWindow.Cursor = Cursors[mResizeMode];
            mInvisibleWindow.Show();

            mUpdateTimer          = new Timer();
            mUpdateTimer.Interval = 25;
            mUpdateTimer.Tick    += UpdateTimer_Tick;
            mUpdateTimer.Start();
        }
コード例 #3
0
        public void Update(Point currentMousePosition)
        {
            int deltaX = currentMousePosition.X - mInitialMousePosition.X;
            int deltaY = currentMousePosition.Y - mInitialMousePosition.Y;

            WinAPI.SetWindowPos(mHwnd, 0,
                                mInitialWindowPosition.X + deltaX,
                                mInitialWindowPosition.Y + deltaY,
                                0, 0, WinAPI.SWP_NOSIZE | WinAPI.SWP_NOZORDER);

            var nearestSide = GetNearestSide(currentMousePosition);

            switch (nearestSide.Side)
            {
            case Side.Left:
                mSnapRectangle.Width    = nearestSide.screen.WorkingArea.Width / 2;
                mSnapRectangle.Height   = nearestSide.screen.WorkingArea.Height;
                mSnapRectangle.Location = nearestSide.screen.WorkingArea.Location;
                break;

            case Side.Top:
                mSnapRectangle.Width    = nearestSide.screen.WorkingArea.Width;
                mSnapRectangle.Height   = nearestSide.screen.WorkingArea.Height / 2;
                mSnapRectangle.Location = nearestSide.screen.WorkingArea.Location;
                break;

            case Side.Right:
                mSnapRectangle.Width    = nearestSide.screen.WorkingArea.Width / 2;
                mSnapRectangle.Height   = nearestSide.screen.WorkingArea.Height;
                mSnapRectangle.Location = nearestSide.screen.WorkingArea.Location.Add(
                    nearestSide.screen.WorkingArea.Width / 2, 0);
                break;

            case Side.Bottom:
                mSnapRectangle.Width    = nearestSide.screen.WorkingArea.Width;
                mSnapRectangle.Height   = nearestSide.screen.WorkingArea.Height / 2;
                mSnapRectangle.Location = nearestSide.screen.WorkingArea.Location.Add(
                    0, nearestSide.screen.WorkingArea.Height / 2);
                break;

            case Side.None:
                break;
            }

            if (nearestSide.Side == Side.None)
            {
                mSnapRectangle.Hide();
            }
            else
            {
                mSnapRectangle.Show();
            }

            mInvisibleWindow.CenterAt(currentMousePosition);
        }
コード例 #4
0
        public void Update(Point currentMousePosition)
        {
            mInvisibleWindow.CenterAt(currentMousePosition);

            var deltaX = currentMousePosition.X - mInitialMousePosition.X;
            var deltaY = currentMousePosition.Y - mInitialMousePosition.Y;

            switch (mResizeMode)
            {
            case ResizeMode.Left:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left + deltaX,
                    mInitialWindowRect.Top,
                    mInitialWindowRect.Width - deltaX,
                    mInitialWindowRect.Height);
                break;

            case ResizeMode.Top:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left,
                    mInitialWindowRect.Top + deltaY,
                    mInitialWindowRect.Width,
                    mInitialWindowRect.Height - deltaY);
                break;

            case ResizeMode.Right:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left,
                    mInitialWindowRect.Top,
                    mInitialWindowRect.Width + deltaX,
                    mInitialWindowRect.Height);
                break;

            case ResizeMode.Bottom:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left,
                    mInitialWindowRect.Top,
                    mInitialWindowRect.Width,
                    mInitialWindowRect.Height + deltaY);
                break;

            case ResizeMode.TopLeft:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left + deltaX,
                    mInitialWindowRect.Top + deltaY,
                    mInitialWindowRect.Width - deltaX,
                    mInitialWindowRect.Height - deltaY);
                break;

            case ResizeMode.TopRight:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left,
                    mInitialWindowRect.Top + deltaY,
                    mInitialWindowRect.Width + deltaX,
                    mInitialWindowRect.Height - deltaY);
                break;

            case ResizeMode.BottomLeft:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left + deltaX,
                    mInitialWindowRect.Top,
                    mInitialWindowRect.Width - deltaX,
                    mInitialWindowRect.Height + deltaY);
                break;

            case ResizeMode.BottomRight:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left,
                    mInitialWindowRect.Top,
                    mInitialWindowRect.Width + deltaX,
                    mInitialWindowRect.Height + deltaY);
                break;

            case ResizeMode.All:
            default:
                mNewWindowRect = new Rectangle(
                    mInitialWindowRect.Left - deltaX,
                    mInitialWindowRect.Top - deltaY,
                    mInitialWindowRect.Width + 2 * deltaX,
                    mInitialWindowRect.Height + 2 * deltaY);
                break;
            }
        }