Example #1
0
        private static void Centering()
        {
            // ペイントの中央揃え
            using (var process = Process.GetProcessesByName("mspaint").FirstOrDefault())
            {
                if (process == null)
                {
                    Console.WriteLine($"No process.");
                    return;
                }
                var windowHandle = process.MainWindowHandle;

                var clientPoint = new NetCoreEx.Geometry.Point(0, 0);
                User32Methods.GetClientRect(windowHandle, out var clientRect);
                User32Methods.ClientToScreen(windowHandle, ref clientPoint);
                Console.WriteLine($"clientRect: {clientRect.ToString()}");
                Console.WriteLine($"clientPoint: {clientPoint.ToString()}");

                var screen = Screen.PrimaryScreen;
                Console.WriteLine($"PrimaryScreen: {screen.Bounds.ToString()}");
                // 小数点以下は切り捨て
                var x = (screen.Bounds.Width - clientRect.Width) / 2;
                var y = (screen.Bounds.Height - clientRect.Height) / 2;
                Console.WriteLine($"Move to ({x}, {y})");

                // Move Window
                User32Methods.MoveWindow(windowHandle, x, y, clientRect.Width, clientRect.Height, true);
            }
        }
        private bool IsCursorInDraggableRegion(IntPtr hWnd, IntPtr lParam)
        {
            Point pt;

            pt.X = NativeMethods.LOWORD(lParam);
            pt.Y = NativeMethods.HIWORD(lParam);

            User32Methods.ClientToScreen(hWnd, ref pt);

            Rectangle rect;

            User32Methods.GetWindowRect(_parentHandle, out rect);

            // Mouse must be within Window
            if (!User32Methods.PtInRect(ref rect, pt))
            {
                return(false);
            }

            Rectangle draggableRegion = new Rectangle
            {
                Left   = rect.Left,
                Top    = rect.Top,
                Right  = rect.Right - _framelessOptions.NoDragWidth,
                Bottom = rect.Top + _framelessOptions.Height
            };

            // Mouse must be within draggable Region
            if (User32Methods.PtInRect(ref draggableRegion, pt))
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Converts a client-area rectangle to screen coordinates.
        /// </summary>
        /// <param name="winInstance">
        /// Handle to the window whose client coordinates are to be converted.
        /// This does mean that this rectangle is expected to be within the window.
        /// </param>
        public void ClientToScreen(IntPtr winInstance)
        {
            Point leftTop     = LeftTop();
            Point rightBottom = RightBottom();

            User32Methods.ClientToScreen(winInstance, ref leftTop);
            User32Methods.ClientToScreen(winInstance, ref rightBottom);

            Left   = leftTop.X;
            Top    = leftTop.Y;
            Right  = rightBottom.X;
            Bottom = rightBottom.Y;
        }