예제 #1
0
 public CenterWindow(IntPtr hOwner)
 {
     this.hOwner             = hOwner;
     this.cbtHook            = new CbtHook();
     cbtHook.WindowActivate += new CbtHook.CbtEventHandler(WndActivate);
     cbtHook.Install();
 }
예제 #2
0
 public void Dispose()
 {
     if (wndProcRetHook != null)
     {
         wndProcRetHook.Uninstall();
         wndProcRetHook = null;
     }
     if (cbtHook != null)
     {
         cbtHook.Uninstall();
         cbtHook = null;
     }
 }
예제 #3
0
 /// <summary>
 /// Displays a message box in front of the specified object and with the specified text.
 /// </summary>
 /// <param name="owner">An implementation of IWin32Window that will own the modal dialog box.</param>
 /// <param name="text">The text to display in the message box.</param>
 /// <param name="caption">The text to display in the title bar of the message box.</param>
 /// <param name="buttons">One of the MessageBoxButtons values that specifies which buttons to display in the message box.</param>
 /// <param name="icon">One of the MessageBoxIcon values that specifies which icon to display in the message box.</param>
 /// <param name="defaultButton">One of the MessageBoxDefaultButton values that specifies the default button for the message box.</param>
 public static DialogResult ShowDialog(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 {
     if (!MessageBox.IsRunningOnMono)
     {
         lock (_syncRoot) {
             if (owner != null)
             {
                 using (CbtHook ch = new CbtHook(owner)) {
                     return((DialogResult)NativeMethods.MessageBox(owner.Handle, text, caption, (uint)buttons | (uint)icon | (uint)defaultButton));
                 }
             }
             else
             {
                 using (CbtHook ch = new CbtHook(null)) {
                     return((DialogResult)NativeMethods.MessageBox(System.IntPtr.Zero, text, caption, (uint)buttons | (uint)icon | (uint)defaultButton));
                 }
             }
         } //lock
     }
     else     //MONO
     {
         return(System.Windows.Forms.MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, 0));
     }
 }
예제 #4
0
        public void WndActivate(object sender, CbtEventArgs e)
        {
            IntPtr hMsgBox = e.wParam;

            // try to find a howner for this message box
            if (hOwner == IntPtr.Zero)
            {
                hOwner = USER32.GetActiveWindow();
            }

            // get the MessageBox window rect
            RECT rectDlg = new RECT();

            USER32.GetWindowRect(hMsgBox, ref rectDlg);

            // get the owner window rect
            RECT rectForm = new RECT();

            USER32.GetWindowRect(hOwner, ref rectForm);

            // get the biggest screen area
            Rectangle rectScreen = API.TrueScreenRect;

            // if no parent window, center on the primary screen
            if (rectForm.right == rectForm.left)
            {
                rectForm.right = rectForm.left = Screen.PrimaryScreen.WorkingArea.Width / 2;
            }
            if (rectForm.bottom == rectForm.top)
            {
                rectForm.bottom = rectForm.top = Screen.PrimaryScreen.WorkingArea.Height / 2;
            }

            // center on parent
            int dx = ((rectDlg.left + rectDlg.right) - (rectForm.left + rectForm.right)) / 2;
            int dy = ((rectDlg.top + rectDlg.bottom) - (rectForm.top + rectForm.bottom)) / 2;

            rect = new Rectangle(
                rectDlg.left - dx,
                rectDlg.top - dy,
                rectDlg.right - rectDlg.left,
                rectDlg.bottom - rectDlg.top);

            // place in the screen
            if (rect.Right > rectScreen.Right)
            {
                rect.Offset(rectScreen.Right - rect.Right, 0);
            }
            if (rect.Bottom > rectScreen.Bottom)
            {
                rect.Offset(0, rectScreen.Bottom - rect.Bottom);
            }
            if (rect.Left < rectScreen.Left)
            {
                rect.Offset(rectScreen.Left - rect.Left, 0);
            }
            if (rect.Top < rectScreen.Top)
            {
                rect.Offset(0, rectScreen.Top - rect.Top);
            }

            if (e.IsDialog)
            {
                // do the job when the WM_INITDIALOG message returns
                wndProcRetHook             = new WndProcRetHook(hMsgBox);
                wndProcRetHook.WndProcRet += new WndProcRetHook.WndProcEventHandler(WndProcRet);
                wndProcRetHook.Install();
            }
            else
            {
                USER32.MoveWindow(hMsgBox, rect.Left, rect.Top, rect.Width, rect.Height, 1);
            }

            // uninstall this hook
            WindowsHook wndHook = (WindowsHook)sender;

            Debug.Assert(cbtHook == wndHook);
            cbtHook.Uninstall();
            cbtHook = null;
        }
예제 #5
0
 /// <summary>
 /// Displays a message box in front of the specified object and with the specified text.
 /// </summary>
 /// <param name="owner">An implementation of IWin32Window that will own the modal dialog box.</param>
 /// <param name="text">The text to display in the message box.</param>
 /// <param name="caption">The text to display in the title bar of the message box.</param>
 /// <param name="buttons">One of the MessageBoxButtons values that specifies which buttons to display in the message box.</param>
 /// <param name="icon">One of the MessageBoxIcon values that specifies which icon to display in the message box.</param>
 /// <param name="defaultButton">One of the MessageBoxDefaultButton values that specifies the default button for the message box.</param>
 public static DialogResult ShowDialog(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
 {
     if (!MessageBox.IsRunningOnMono) {
         lock (_syncRoot) {
             if (owner != null) {
                 using (CbtHook ch = new CbtHook(owner)) {
                     return (DialogResult)NativeMethods.MessageBox(owner.Handle, text, caption, (uint)buttons | (uint)icon | (uint)defaultButton);
                 }
             } else {
                 using (CbtHook ch = new CbtHook(null)) {
                     return (DialogResult)NativeMethods.MessageBox(System.IntPtr.Zero, text, caption, (uint)buttons | (uint)icon | (uint)defaultButton);
                 }
             }
         } //lock
     } else { //MONO
         return System.Windows.Forms.MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, 0);
     }
 }