Пример #1
0
        internal void MoveToScreenCenter(HandleRef hWnd)
        {
            // Create an IntPtr to store a handle to the monitor.
            IntPtr hMonitor = IntPtr.Zero;

            // Get the monitor to use based on the location of the parent window
            if (_hwndOwnerWindow != IntPtr.Zero)
            {
                // we have a owner hwnd; center on the screen on
                // which our owner hwnd is.
                // We use MONITOR_DEFAULTTONEAREST to get the monitor
                // nearest to the window if the window doesn't intersect
                // any display monitor.
                hMonitor = SafeNativeMethods.MonitorFromWindow(
                    new HandleRef(this, _hwndOwnerWindow),                   // window to find monitor location for
                    NativeMethods.MONITOR_DEFAULTTONEAREST);                 // get the monitor nearest to the window


                // Only move the window if we got a valid monitor... otherwise let Windows
                // position the dialog.
                if (hMonitor != IntPtr.Zero)
                {
                    // Now, create another RECT and fill it with the bounds of the parent window.
                    NativeMethods.RECT dialogRect = new NativeMethods.RECT();
                    SafeNativeMethods.GetWindowRect(hWnd, ref dialogRect);

                    Size dialogSize = new Size((dialogRect.right - dialogRect.left),  /*width*/
                                               (dialogRect.bottom - dialogRect.top)); /*height*/

                    // create variables that will receive the new position of the dialog
                    double x = 0;
                    double y = 0;

                    // Call into a static function in System.Windows.Window to calculate
                    // the actual new position
                    Window.CalculateCenterScreenPosition(hMonitor,
                                                         dialogSize,
                                                         ref x,
                                                         ref y);

                    // Call SetWindowPos to actually move the window.
                    UnsafeNativeMethods.SetWindowPos(hWnd,                          // handle to the window to move
                                                     NativeMethods.NullHandleRef,   // window to precede this one in zorder
                                                     (int)Math.Round(x),
                                                     (int)Math.Round(y),            // new X and Y positions
                                                     0, 0,                          // new width and height, if applicable
                                                                                    // Flags:
                                                                                    //    SWP_NOSIZE: Retains current size
                                                                                    //    SWP_NOZORDER:  retains current zorder
                                                                                    //    SWP_NOACTIVATE:  does not activate the window
                                                     NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE);
                }
            }
        }
Пример #2
0
        internal void MoveToScreenCenter(HandleRef hWnd)
        {
            IntPtr intPtr = IntPtr.Zero;

            if (this._hwndOwnerWindow != IntPtr.Zero)
            {
                intPtr = SafeNativeMethods.MonitorFromWindow(new HandleRef(this, this._hwndOwnerWindow), 2);
                if (intPtr != IntPtr.Zero)
                {
                    NativeMethods.RECT rect = default(NativeMethods.RECT);
                    SafeNativeMethods.GetWindowRect(hWnd, ref rect);
                    Size   currentSizeDeviceUnits = new Size((double)(rect.right - rect.left), (double)(rect.bottom - rect.top));
                    double a  = 0.0;
                    double a2 = 0.0;
                    Window.CalculateCenterScreenPosition(intPtr, currentSizeDeviceUnits, ref a, ref a2);
                    UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.NullHandleRef, (int)Math.Round(a), (int)Math.Round(a2), 0, 0, 21);
                }
            }
        }