Exemple #1
0
        private static IntPtr MessageBoxHookProc(Int32 nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CenteredDialogBox.CallNextHookEx(windowHook, nCode, wParam, lParam));
            }

            CWPRETSTRUCT msg  = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
            IntPtr       hook = windowHook;

            if (msg.Message == (Int32)CbtHookAction.HCBT_ACTIVATE)
            {
                try
                {
                    CenteredDialogBox.CenterWindow(msg.Hwnd);
                }
                finally
                {
                    CenteredDialogBox.UnhookWindowsHookEx(windowHook);
                    windowHook = IntPtr.Zero;
                }
            }

            return(CenteredDialogBox.CallNextHookEx(hook, nCode, wParam, lParam));
        }
Exemple #2
0
        private static void CenterWindow(IntPtr childWindowHandle)
        {
            Rectangle recChild = new Rectangle(0, 0, 0, 0);
            Boolean   success  = GetWindowRect(childWindowHandle, ref recChild);

            Int32 width  = recChild.Width - recChild.X;
            Int32 height = recChild.Height - recChild.Y;

            Rectangle parentRectangle = new Rectangle(0, 0, 0, 0);

            success = CenteredDialogBox.GetWindowRect(ownerPtr, ref parentRectangle);

            System.Drawing.Point centerPoint = new System.Drawing.Point(0, 0);
            centerPoint.X = parentRectangle.X + ((parentRectangle.Width - parentRectangle.X) / 2);
            centerPoint.Y = parentRectangle.Y + ((parentRectangle.Height - parentRectangle.Y) / 2);

            System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
            startPoint.X = centerPoint.X - (width / 2);
            startPoint.Y = centerPoint.Y - (height / 2);

            startPoint.X = (startPoint.X < 0) ? 0 : startPoint.X;
            startPoint.Y = (startPoint.Y < 0) ? 0 : startPoint.Y;

            Int32 result = CenteredDialogBox.MoveWindow(childWindowHandle, startPoint.X, startPoint.Y, width, height, false);
        }
Exemple #3
0
        private static void Initialize()
        {
            if (windowHook != IntPtr.Zero)
            {
                throw new NotSupportedException("Multiple calls are not supported.");
            }

            if (ownerPtr != null)
            {
                ProcessThread processThread = Process.GetCurrentProcess().Threads
                                              .OfType <ProcessThread>()
                                              .FirstOrDefault(thread => thread.ThreadState == ThreadState.Running);

                if (processThread != null)
                {
                    windowHook = CenteredDialogBox.SetWindowsHookEx(WH_CALLWNDPROCRET, hookProc, IntPtr.Zero, processThread.Id);
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Shows a dialog box with the specified parameters.
 /// </summary>
 /// <param name="owner">The creator of this messagebox, on which we will center this.</param>
 /// <param name="text">The body text.</param>
 /// <param name="caption">The window caption.</param>
 /// <param name="buttons">The buttons choices to display</param>
 /// <param name="icon">The icon to display.</param>
 /// <returns>The result based on the button pressed.</returns>
 public static MessageBoxResult Show(Window owner, String text, String caption, MessageBoxButton buttons, MessageBoxImage icon)
 {
     ownerPtr = new WindowInteropHelper(owner).Handle;
     CenteredDialogBox.Initialize();
     return(MessageBox.Show(owner, text, caption, buttons, icon));
 }